Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Implicit assignment to $_

by SimplySimple (Initiate)
on Feb 26, 2013 at 15:34 UTC ( #1020708=perlquestion: print w/ replies, xml ) Need Help??
SimplySimple has asked for the wisdom of the Perl Monks concerning the following question:

I'm a beginner to Perl, and I have a question about assigning values to the variable $_. I'm trying to get "Test A" as the output and I get no output instead. And here is the slice of code I want to fix.

"Test A"or"Test B"; print;

May someone help me to modify the code to get the expected result? Thanks a lot.

Comment on Implicit assignment to $_
Download Code
Re: Implicit assignment to $_
by arnaud99 (Beadle) on Feb 26, 2013 at 16:08 UTC

    Hi, I am unsure about what you are trying to achieve here as there is very little of the code.

    $_ is a global variable and quite a few places in perl use $_ as the default.

    To know more use 'perldoc perlvar' and you will have a good explanation of where and how $_ can be used.

    Below is a small program that uses $_ as the default for both the while loop and the print statement.

    I hope this helps.

    Arnaud.

    use strict; use warnings; use autodie; while(<DATA>) { print; } __DATA__ Test A Test B
Re: Implicit assignment to $_
by blue_cowdawg (Prior) on Feb 26, 2013 at 16:10 UTC

    This is a prime example of why I preach so heavily about use strict;. If you had invoked that pragma you'd have seen the warning:

    Use of uninitialized value $_ in print at sopw-simple.pl line 6.
    Quoting from Gabor Szabo on his website http://perl5maven.com/the-default-variable-of-perl:
    In Perl, several functions and operators use this variable as a default, in case no parameter is explicitly used. In general, I'd say you should NOT see $_ in real code. I think the whole point of $_ is that you don't have to write it explicitly.
    
    Well, except when you do.
    
    Having a default variable is a very powerful idea, but using it incorrectly can reduce the readability of your code.
    
    Just for grins I tried:
    $_ = "TestA" or "TestB";
    and Perl was not happy with that issuing:
    Found = in conditional, should be == at ./sopw-simple.pl line 4. Useless use of a constant (TestB) in void context at ./sopw-simple.pl +line 4. TestA

    My suggestion is read the web page I provided above for some hints on what you can and can't do with $_. Just a thought...


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

      Despite all correct (!!) advices given before:

      print for ("Test A" or "Test B");

      Best regards
      McA

      "This is a prime example of why I preach so heavily about use strict;. If you had invoked that pragma you'd have seen the warning:

      "Use of uninitialized value $_ in print at sopw-simple.pl line 6."

      Nuh-uh... strict does no such thing.

      strict does three things:

      • Throws compile-time errors for undeclared variables.
      • Throws compile-time errors when barewords are seen which don't correspond to subs, and don't look like the package name in a class method call.
      • Throws run-time errors when symbolic references are used, or more generally when trying to dereference a variable which does not contain a reference.

      strict never issues any warnings. And strict doesn't care about variables being undef (unless you try to dereference them).

      package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
            Nuh-uh... strict does no such thing.

        Really? That's a copy/paste from my terminal. Wish there was a way to post screenshots to PM.


        Peter L. Berghold -- Unix Professional
        Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: Implicit assignment to $_
by grondilu (Pilgrim) on Feb 26, 2013 at 16:25 UTC

    Your code:

    "Test A"or"Test B"; print;
    suggests you think $_ refers to the last evaluated expression. It does not. $_ is not python's underscore in interactive mode.

Re: Implicit assignment to $_
by tobyink (Abbot) on Feb 26, 2013 at 16:59 UTC
    $_ = ("Test A" or "Test B"); print;
    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
Re: Implicit assignment to $_
by pvaldes (Chaplain) on Feb 26, 2013 at 17:05 UTC

    I'm trying to get "Test A"

    print "Test A" or die "Test B is not acceptable, its purpose here is merely esoteric";

    (Yes, you are doing wrong) Is "Test A" a six character's string, a filehandle, a variable, a macguffin?

    print  $test_A;     # looking for this?

    We simply need some more clues

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2014-07-10 03:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (198 votes), past polls