Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

perlapp, die, and __DATA__

by joker0927 (Novice)
on Apr 25, 2003 at 04:18 UTC ( [id://253065]=perlquestion: print w/replies, xml ) Need Help??

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

OK, I'm a newbie; so please don't laugh too loud or flame me here, but this one is driving me crazy.

The goal here is to be able to pass /? to my perl app as a command line arg and have it return the text that follows __DATA__. To save space, I've only listed the pertinent code. And yes, I've tested this portion only with the same results.

The following code works:
use strict; package SAR; &get_args (@ARGV); sub get_args { my @help = <DATA>; ($ARGV[0] eq "/?") && (die @help); } return 1; __DATA__ testing 1 testing 2 testing 3
When ran as c:>test.pl /?
It returns:

testing 1
testing 2
testing 3

as expected.

However, after turning it into an exe with perlapp it no longer works. Running c:>test.exe /? will return "Died at test.pl line 8." unless I wrap the @help array in quotes, then it just returns a few blank lines and goes back to the prompt.
I've tried including the package (i.e. SAR::DATA) thinking it might have something to do with the fact that perlapp is adding a lot of other modules and maybe it's getting confused about which module to pull DATA from, but that didn't work. It gives the same results.

I promise I've done my homework. I searched this website, activestates documentation, perldoc, and several books on perl. I just can't find an answer anywhere. Any insight here would be greatly appreciated.

Replies are listed 'Best First'.
Re: perlapp, die, and __DATA__
by grep (Monsignor) on Apr 25, 2003 at 04:36 UTC
    You can't use the <DATA> filehandle with perlapp.

    Also couple of comments on your code:

    use strict; package SAR; &get_args (@ARGV); #Ok here you're passing a copy #of @ARGV into get_args's @_. #BTW your don't need & the #trailing ()'s make strict happy sub get_args { my @help = <DATA>; #Why read this now #You don't know if you need #it yet. ($ARGV[0] eq "/?") && (die @help); #Ok you passed the contents of #@ARGV into your sub in @_ but #you ignore it and read the #global @ARGV directly. #copy @_ that way you don't have #to worry about modifing the #global @ARGV. #my @array = @_; #die @help if ($array[0] eq "/?") }
    UPDATE: integral is absolutely right, and I'm in a case of do what I mean not what I say :(. Updated comment to correct.

    grep
    Mynd you, mønk bites Kan be pretti nasti...

      Perl passes by alias, so using @_ instead of @ARGV will not prevent worries about modifying the global @ARGV. Options are to make a lexical copy (my @args = @ARGV) or to localise (local @ARGV = @ARGV).

      use strict; use warnings; print "1: @ARGV\n"; foo(@ARGV); print "3: @ARGV\n"; sub foo { print "2a: @_\n"; $_[0] = "foobar"; print "2b: @_\n"; }

      (Note however that the elements are aliased indivualluy into @_ not the whole array, so if @ARGV is empty there will be no modification as you are then adding a new element to @_ instead of modifying an element in @ARGV through @_.)

      --
      integral, resident of freenode's #perl
      
Re: perlapp, die, and __DATA__
by JamesNC (Chaplain) on Apr 25, 2003 at 08:54 UTC
    I wanted to let you know that __DATA__ doesn't work in Tinyperl either. Your code isn't broke... You can create a data file and load it into your app, or just roll your own array to simulate __DATA__ like so...
    my @DATA; my @data = q( testing 1 testing 2 testing 3 ); for (@data) { push @DATA, split /\n/; } $DATA[$_].="\n" for 0..$#DATA; for(@DATA){ chomp; print; }

    it's all about preference :) Lot of ways to do the same thing.. James

      Or since @DATA isn't needed as an array this would be sufficient:

      my $data = q{ line 1 line 2 line 3 }; # OR $data = <<'END'; line 1 line 2 line 3 END # to ensure a terminating newline # chomp $data; $data .= $/ print "$data";

      This is as usual another way to write the first suggestion (where @data should be $data too):

      @DATA = map "$_\n", split /\n/, $data; # but why add \n when you chomp + later?

      --
      integral, resident of freenode's #perl
      
Re: perlapp, die, and __DATA__
by Anonymous Monk on Apr 25, 2003 at 04:48 UTC
    OK, I'm a newbie; so please don't laugh too loud or flame me here, but this one is driving me crazy

    Nobody really cares if you're a newbies, we are not here to flame, so no need to draw attention that way.

    Have you read the perlapp documentation? I'm sure it will tell you that this doesn't work.

Re: perlapp, die, and __DATA__
by joker0927 (Novice) on Apr 25, 2003 at 17:59 UTC
    Thank you all for the feedback. I actually found that mention in the perlapp faq right after I posted this. I'm glad I posted though. You monks have given me some good perls of wisdom. Thanks.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2024-04-18 02:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found