Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

using 'my $var' vs. 'my($var)'

by decnartne (Beadle)
on Oct 26, 2000 at 18:03 UTC ( [id://38605]=perlquestion: print w/replies, xml ) Need Help??

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

i must be missing something... (ever the initiate)

please consider the following snippet:

#!/usr/bin/perl -w use strict; my $config_file = "/home/foo/bar"; read_conf($config_file); # el fin # this subroutine in RL actually does something, # but that is outside the scope of the question ;) sub read_conf { my $conf_file = @_; open (CONF, "$conf_file") or die "[ $conf_file ]: $!\n"; }


upon calling read_conf(), i get the die msg:

"[ 1 ]: No such file or directory"

now, if i change my $conf_file = @_; to: my ($conf_file) = @_; it works fine.

what *am* i missing?

thanks in advance



decnartne ~ entranced

Replies are listed 'Best First'.
Re: using 'my $var' vs. 'my($var)'
by Fastolfe (Vicar) on Oct 26, 2000 at 18:11 UTC
    It's all about forcing a context.
    @array = qw/ a b c /; $scalar = @array; # puts @array into scalar context print $scalar; # '3' ($scalar) = @array; # forces @array into list context print $scalar; # 'a' ($a, $b, $c) = @array; # likewise print "$a, $b, $c"; # 'a, b, c'
    The use of 'my' doesn't change the way parens (or lack thereof) affect the context of the variables. You've figured out that shift returns a single scalar: the left-most item in @array. The difference between that and the 2nd method above (what you were using previously), is that the array itself is unchanged. Shift modifies it, so subsequent calls will get you something different, whereas the assignment in the list context above will always give you the same values.

    This also is what allows us to do things like this:

    ($a, $b) = ($b, $a); # swap print "$a, $b, $c"; # 'b, a, c'
Re: using 'my $var' vs. 'my($var)'
by mirod (Canon) on Oct 26, 2000 at 18:18 UTC

    Here is the difference, plus another way to write it.

    my $conf_file= @_
    creates the $conf_file scalar variable and sets it to the value of @_ in scalar context, which is... 1, hence the error message when your script tries to open the1 file.

    my( $conf_file)= @_;
    creates the scalar variable $conf_file and puts it in a list (as the first and only element of that list). Then it sets that list with @_, which means that the first element in that list ($conf_file) gets the first element of @_. Which works fine when called with just one element, but maybe not for the reasons you thought.

    You could also have written
    my $conf_file= shift;
    which declares the scalar variable $conf_file and removes the first element from @_ and sets $conf_file with it.

    Which leads me to a style question to my fellow monks: do you usually use
    my( $arg1, $arg2)= @_;
    or
    my $arg1= shift;
    my $arg2= shift;
    ?

    The first option is more compact but the second one lets you check for extra (unwanted) arguments if you want.

    I usually use the my( $arg1, $arg2)= @_; form, but maybe it's just lazyness...

      I use both. Depends on what I want. =) I tend to use the more compact form unless as you mentioned I'm going to provide alternates. One reason for using shift is if I'm going to do the elder-god level evil stunt of &subname and want to pass on all the other args to a second sub (that I choose based on the first arg).

      In these modern, enlightened times, no one believes in that sort of nightmare anymore, we have gentler, happier gods... and references... and we read the rest of the camel book...

      sub eek { my ($selection)=@_; $gsubhash{$selection}->(@_[1..$#_]); }

      Of course, as I get older that seems like too much work. Now I just write one big linear script... =)

      UPDATE Grr... thanks merlyn! As I recall, that is the mistake I had to fix a couple years ago when I actually used that idiom =)

      --
      $you = new YOU;
      honk() if $you->love(perl)

Re: using 'my $var' vs. 'my($var)'
by redcloud (Parson) on Oct 26, 2000 at 18:13 UTC
    @_ is evaluated in a scalar context in the first example. So, you're getting the number of entries in @_ in $conf_file instead of the actual parameter that you passed in.
RE: using 'my $var' vs. 'my($var)'
by decnartne (Beadle) on Oct 26, 2000 at 18:07 UTC
    ack! forgive the spam - i just needed to rtfm...

    my $var = shift(@_); # ;) <BR<BR<BR
    decnartne ~ entranced
      my $var = shift(@_);

      You can use the shorter, more idiomatic:

      my $var = shift;

      The @_ is implied.

      Cheers,
      KM

Re: using 'my $var' vs. 'my($var)'
by decnartne (Beadle) on Oct 26, 2000 at 20:20 UTC
    thanks, all, for the feedback - and for the flame-less tone as well

    decnartne ~ entranced
RE: using 'my $var' vs. 'my($var)'
by JeffreyD (Initiate) on Oct 28, 2000 at 03:55 UTC
    You are evaluating @_ in a scalar context! When you say my $conf_file = @_; you are putting the number of elements stored in the @_ list (i.e. "1") into the variable $conf_file. When you say my ($conf_file) = @_; you are evaluating @_ in an array context because what is on the LHS of the assignment is an array. So the element in @_ is copied over to $conf_file. You could say something like: my $conf_file = shift; to shift an element off of @_ into $conf_file if you don't like the parens! -- jeff
RE: using 'my $var' vs. 'my($var)'
by Anonymous Monk on Oct 28, 2000 at 06:14 UTC
    @_ is an array so assigning it a scalar doesn't work (at least I don't think it does). By placing the scalar value in parentheses you are effectively assigning the first value of the array to the scalar.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2025-03-18 04:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (56 votes). Check out past polls.