Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

syntax questions

by manishrathi (Beadle)
on Mar 06, 2013 at 22:08 UTC ( #1022103=perlquestion: print w/ replies, xml ) Need Help??
manishrathi has asked for the wisdom of the Perl Monks concerning the following question:

if($templateName eq 'Deploy SELECTED WORKAREA FILES to DEV'){ $filelist = "$iwhome/tmp/filelist-${wfid}.txt"; my @files = $task->GetFiles(); open FILELIST, ">$filelist"; foreach my $file (@files){ print FILELIST "$file\n"; } close FILELIST; &deployContent($sourcePath, $targetPath, $iwodclient_cfg,$workarea +Name,$filelist);......
What does ${wfid} mean in here ? Is it same as $wfid ? If yes then why use this syntax ?
Is file handler FILELIST adding members to $filelist ?

&deployContent($sourcePath, $targetPath, $iwodclient_cfg,$workareaName,$filelist);
In this code, deployContent subroutine is called. But why is it preceded by & ? If we dont use & before deployContent subroutine, will it not be called ?


Comment on syntax questions
Select or Download Code
Replies are listed 'Best First'.
Re: syntax questions
by toolic (Bishop) on Mar 06, 2013 at 22:16 UTC
    What does ${wfid} mean in here ? Is it same as $wfid ? If yes then why use this syntax ?
    In your code, yes, ${wfid} is the same as $wfid. You would need the curlies if you had this instead (I deleted the dot):
    $filelist = "$iwhome/tmp/filelist-${wfid}txt";

    Without the curlies, you'd have a different variable: $wfidtxt:

    $filelist = "$iwhome/tmp/filelist-$wfidtxt";

    Refer to Scalar value constructors

    Is file handler FILELIST adding members to $filelist ?
    FILELIST is a file handle. Your open statement opens the file named $filelist for output. The print adds lines to the file.

    Generally, you need not use &. Refer to Prototypes for gory details.

Re: syntax questions
by rjt (Deacon) on Mar 07, 2013 at 01:49 UTC

    While my esteemed predecessors have done a good job of answering your direct questions, perhaps I can shed some light on a few stylistic questions you didn't ask, but may still find informative.

    The line:

         open FILELIST, ">$filelist";

    is problematic for a couple of reasons:

    • The open might fail (for example, if the file does not exist), so you need to check the return.
    • The two-argument open (with the append (>) symbol in the same string as the filename) is a potential security risk; see the old Two-arg open() considered dangerous discussion for more info.
    • The filehandle, FILELIST, is in the global symbol table. It's better form to create a lexical variable instead with my.

    So, instead, I would write:

    (UPDATED thanks to AnomylousMonk's good catch of a very carelessly chosen variable name)

    open my $fh, '>', $filelist or die "Can't open $filelist: $!";

    If it fails, the program will exit with an informative error code. You could instead wrap the open call in an if statement if you want to do something else.

    Purely personal preference on this next one, but your entire foreach { ... } loop could be more concisely written:

        print $fh $_ for @files;
      open my $filelist, '>', $filelist or die "Can't open $filelist: $!";

      This is simply an inadvertance, but using a new lexical  my $filelist for the filehandle in the open statement masks the file name in the old lexical of the same name (and also generates a warning if warnings are enabled, which is strongly recommended). I would write something like:

      >perl -wMstrict -le "my $filelist = 'xyzzy'; open my $filehandle, '>', $filelist or die qq{opening '$filelist': $! +}; print $filehandle $_ for qw(foo bar baz); close $filehandle or die qq{closing '$filelist': $!}; " >type xyzzy foo bar baz
Re: syntax questions
by igelkott (Curate) on Mar 06, 2013 at 22:25 UTC

    Sometimes it's useful to be clarify what's part of the variable name and what isn't, especially in strings. In this case, it wasn't needed but if it was ${wfid}_1.txt, it would be necessary.

    file handler FILELIST adding members to $filelist

    Not quite. FILELIST represents the file (given in $filelist) and the "foreach" loop is adding to the file identified by FILELIST. Maybe it's just semantics but that's the way I read it.

    why is it preceded by &
    Using the & for function calls is archaic and seriously deprecated. Is this code from Perl 4?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1022103]
Approved by toolic
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2016-05-27 01:17 GMT
Find Nodes?
    Voting Booth?