Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Variable Declaration

by pp (Novice)
on Aug 31, 2007 at 14:36 UTC ( [id://636339]=perlquestion: print w/replies, xml ) Need Help??

pp has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Variable Declaration
by Joost (Canon) on Aug 31, 2007 at 14:50 UTC
      Here is the code:
      use File::Basename; $infile_name = shift(@ARGV); if ( ! -r $infile_name) { print STDERR "1.Cannot read $infile_name\n"; exit 99; }
      a few more lines in between and in the main program I have the following:
      my $ver_name = shift @ARGV; my $mon_dir = dirname($infile_name); # Get directory if (! -z $ver_name) { (our $graph_dir = $mon_dir) =~ s?log/.*$?abinitio/run?; #Build dire +ctory as above if not equal to version.(Replace log/* with abinitio/r +un) } else { (our $graph_dir = $mon_dir)=~ s?log/.*$?$ver_name/abinitio/run?; #Build directory as such if equal to verion(Replace log/* with $ver_na +me/abinitio/run) } my $graph = "$graph_dir" . "/" . "$graphname"; # Concatenate graph directory with graphname if ( ! -r $graph) { print STDERR "2. Cannot read graph=$graph\n" exit 99; }
      By including the if-then-else condition I am getting the following error that it "Cannot read graph=********(pathname)
        A second error you haven't noticed yet: $? is a variable name. Perhaps you wanted \$?, although .*\$? is the same as just .*.
Re: Variable Declaration
by derby (Abbot) on Aug 31, 2007 at 14:51 UTC

    Please use code tags. I think you want (not valid perl):

    my $var2; if <cond1> { $var2 = a; } else { $var2 = y; }

    -derby

      The following would also work:

      my $var2 = <cond1> ? a : y;

      But be careful. Even slight complexity in its arguments can make it hard to read.

        A third option is
        my $var2 = do { if ( <cond1> ) { a; } else { y; } };

        All three constructs are more-or-less interchangable and which one you use should be decided based on which is most readable in any particular instance.

Re: Variable Declaration
by Roy Johnson (Monsignor) on Aug 31, 2007 at 15:09 UTC
    $var1 is defined in the scope of the if-blocks. It is not available outside them. Your example here is probably better written as
    my $var2 = (cond 1) ? 'a' : 'y';
    Downvotes without explanation continue to be the scourge of Perlmonks.

    Caution: Contents may have been coded under pressure.
Re: Variable Declaration
by Anonymous Monk on Aug 31, 2007 at 15:06 UTC
    The problem was solved by others before I got a crack at it but I couldn't resist explaining. It's not often I see something on here I could solve. You've got a classic beginner problem. This is scoping. Since you declared the variables (using "my") inside the if statement it only exists inside the if statement. So as soon as you leave your if statement you no longer have a value in $var1 so basically you assign nothing to $var2.

    Get Learning Perl and a big package of little yellow sticky notes to flag your favorite pages.

    gj

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://636339]
Approved by Joost
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (None)
    As of 2024-04-25 01:43 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found