Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: How to use __DATA__ efficiently, help!

by Anonyrnous Monk (Hermit)
on Feb 09, 2011 at 19:14 UTC ( #887254=note: print w/ replies, xml ) Need Help??


in reply to How to use __DATA__ efficiently, help!

#do{$data = $data . $_ } while (<DATA>); #gives a uninitialized value +in concatenation

You get the "uninitialized value" warning because the do {...} executes once before a value is being assigned to $_.  Just turn it around:

my $data; while (<DATA>) { $data .= $_ }

BTW, note that $data .= "foo" (or $data = $data . "foo" for that matter) is a special case that does not generate an "uninitialzed value" warning, even if $data is undefined initially:

$ perl -we 'my $foo; $foo = $foo . "foo"' # no warning

while this does, of course:

$ perl -we 'my $foo; my $bar = $foo . "foo"' Use of uninitialized value $foo in concatenation (.) or string at -e l +ine 1.


Comment on Re: How to use __DATA__ efficiently, help!
Select or Download Code
Re^2: How to use __DATA__ efficiently, help!
by Anonymous Monk on Feb 09, 2011 at 19:26 UTC
    I found a way to " values of variables ($date - $location) into the html inside the "__DATA__"". I can do this, just hope I am not breaking any rules:
    while (<DATA>) { $_.=s/<--date\/\/>/$date/; $_.=s/<--loc\/\/>/$location/; $data .= $_ }
    But how to use multiple sets of "__DATA__" still stands.
      But how to use multiple sets of "__DATA__" still stands.

      The answer is simple: you can't. There's only one __DATA__ section. At least in the main package.

      In other words, you'd have to use some separator, e.g. an empty line (if that doesn't occur otherwise in the data), and then split it up into sets yourself.

      You could in theory put different __DATA__ sections in different modules (i.e. different packages in different files), and then access them as Foo::DATA, etc., but you might as well just put the data sets in normal files and read them from there...

      # File Set1.pm package Set1; 1; __DATA__ foo1 foo2 foo3 # File Set2.pm package Set2; 1; __DATA__ bar1 bar2 bar3 # main script #!/usr/bin/perl -w use strict; use Set1; use Set2; print "Set 1:\n"; while (<Set1::DATA>) { print } print "Set 2:\n"; while (<Set2::DATA>) { print }

      Output:

      Set 1: foo1 foo2 foo3 Set 2: bar1 bar2 bar3
        Namespacing your __DATA__s is a clever trick, but you're wrong in saying that __DATA__ can't be accessed twice. You can use __DATA__ twice: just rewind it. You need to do so carefully:

        #!/usr/bin/perl use strict; use warnings; my $data_start = tell DATA; print while (<DATA>); seek DATA, $data_start, 0; print while (<DATA>); __DATA__ hello world

        If you don't bother to tell DATA; ahead of time and instead just seek DATA, 0, 0;, then you effectively print "hello world\n`cat $0`" which isn't what you're expecting.

        Is this the best solution to the original poster's problem? Nope. Is it a solution to "how do I re-process this normal filehandle?" Yep.

      I am getting weird numbers in the middle of the html code; 1 and 11, using:
      while (<DATA>) { $_.=s/<--date\/\/>/$date/; $_.=s/<--loc\/\/>/$location/; $data .= $_ }
      Any ideas why?
        $_.=s/<--date\/\/>/$date/;

        You probably meant

        $_ =~ s/<--date\/\/>/$date/; ...

        or just (as $_ is the default anyway)

        s/<--date\/\/>/$date/; ...

        s/// returns the number of substitutions made, so as you have it, you're appending that number to $_.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (13)
As of 2014-08-22 14:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (158 votes), past polls