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

Re: Extracting text from a line

by almut (Canon)
on Jul 12, 2010 at 00:49 UTC ( #848906=note: print w/ replies, xml ) Need Help??


in reply to Extracting text from a line

my ($uid) = $string =~ /^UID: (\w+)/; print $fh $uid if $uid;

(where $fh is a file handle opened for writing to the other file)


Comment on Re: Extracting text from a line
Select or Download Code
Re^2: Extracting text from a line
by targetsmart (Curate) on Jul 12, 2010 at 05:47 UTC
    print $fh $uid if $uid;

    you meant to say
    print $fh $1 if $uid;
    I guess.

    update almut was right
    I thought it was scalar context, missed to notice that "(" ")" => "list context" ,my mistake :-).
    Thanks to almut and pemungkah



    Vivek
    -- 'I' am not the body, 'I' am the 'soul/consciousness', which has no beginning or no end, no attachment or no aversion, nothing to attain or lose.

      What's the difference, semantically?

      Note that in list context (my ($uid) = ...), the match operation returns the captures, so $uid holds the same value as $1.  If there is no match, both values are undef.

        Vivek, I strongly disagree with you here because of the fact that the number variables are very fragile: they are global to every pattern match.

        If someone else comes along and adds (for instance) a"harmless" subroutine call that sometimes does a pattern match of its own between the match and the usage of $1, then the results are unpredictable because the intervening match has reused the global variables.

        If you always capture the value in a separate variable immediately, you're safe from this potential bug.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2015-07-06 21:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (83 votes), past polls