tags:

views:

117

answers:

4

What does the below statement say exactly?

my @dirs = qw(fred|flintstone <barney&rubble> betty );

The complete story is:

my $tarfile = "something*wicked.tar";
my @dirs = qw(fred|flintstone <barney&rubble> betty );
system "tar", "cvf", $tarfile, @dirs;

This has been taken from Learning Perl, 4th Edition.

The result that the system command will run on shell is:

tar cvf fred|flintstone <barney&rubble> betty

But does this command has a meaning on unix?

+4  A: 

qw() splits the string between parentheses through whitespaces (spaces, tabs, any number of them) and returns a list: "fred|flintstone", "<barney&rubble>", "betty"

EDIT: hint from @kemp: it returns a list

And now to your updated question:

tar cvf fred|flintstone <barney&rubble> betty

Yes, the characters |, <, > and & do have meanings in Linux:

| redirects the standard output from tar cvf fred to the standard input of flintstone.

< sends the file barney to the standard input of flintstone

& runs the previous command and sends it to background

> writes the standard output of rubble to the file betty

Whether the whole line has a meaning, it depends on individual programs.

eumiro
Technically `qw` creates a list, not an array
kemp
yes i agree.please see the edit
Vijay Sarathi
well,evryone here is correct but i choose the one with least points.so others ..please dont mind
Vijay Sarathi
Ven'Tatsu
+3  A: 

It creates a list of the following values:

fred|flinstone
<barney&rubble>
betty

and assigns it to the array @dirs.

qw stands for "quote words" so it creates a list of the values given using a white space as separator.

Regarding your edit: the unix command tar creates an archive of the given files. cvf are flags to control its behavior:

c - create archive
v - verbose mode
f - use following argument as archive name

next comes the name of the file you want to save the archive to, and a list of other files to be included in it.

kemp
yes i agree.please see the edit
Vijay Sarathi
+5  A: 

From perldoc perlop:

qw/STRING/
           Evaluates to a list of the words extracted out of STRING, using
           embedded whitespace as the word delimiters.  It can be understood
           as being roughly equivalent to:

               split(' ', q/STRING/);

           the differences being that it generates a real list at compile
           time, and in scalar context it returns the last element in the
           list.  So this expression:

               qw(foo bar baz)

           is semantically equivalent to the list:

               'foo', 'bar', 'baz' 
eugene y
yes i agree.please see the edit
Vijay Sarathi
+2  A: 

You can use Data::Dumper to look what dirs looks like:

$ perl -e 'use Data::Dumper; my @dirs = qw(fred|flintstone <barney&rubble> betty ); print Dumper(@dirs);'

$VAR1 = 'fred|flintstone';
$VAR2 = '<barney&rubble>';
$VAR3 = 'betty';

Or thanks to Eugene:

$ perl -e 'use Data::Dumper; my @dirs = qw(fred|flintstone <barney&rubble> betty ); print Dumper \@dirs;'
$VAR1 = [
          'fred|flintstone',
          '<barney&rubble>',
          'betty'
        ];

which is even better!

Quote_and_Quote_like_Operators is helpful too. It reminds qq qw and so.

Aif
Dumping a reference can be more descriptive: `print Dumper \@dirs;`
eugene y