[23846] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 6049 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jan 29 22:52:53 2004

Date: Thu, 29 Jan 2004 19:46:22 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Thu, 29 Jan 2004     Volume: 10 Number: 6049

Today's topics:
        update file in real time via CGI script <parimi@nowhere.none.com>
    Re: update file in real time via CGI script (Walter Roberson)
        use of stat and argument isn't numeric message <ya_perl_dude@yahoo.com>
    Re: use of stat and argument isn't numeric message <usenet@morrow.me.uk>
    Re: use of stat and argument isn't numeric message <mbroida@fake.domain>
    Re: use of stat and argument isn't numeric message <gnari@simnet.is>
    Re: use of stat and argument isn't numeric message <mbroida@fake.domain>
    Re: use of stat and argument isn't numeric message <gnari@simnet.is>
    Re: use of stat and argument isn't numeric message (Anno Siegel)
        using a message box - some questions. <no_spam@no_spam.com>
    Re: using a message box - some questions. <ittyspam@yahoo.com>
    Re: using a message box - some questions. <no_spam@no_spam.com>
    Re: using a message box - some questions. <1usa@llenroc.ude>
    Re: using a message box - some questions. <jurgenex@hotmail.com>
    Re: using a message box - some questions. <no_spam@no_spam.com>
        Using DATA with Multiple Input Files <mshelor@comcast.removeme.net>
    Re: Using DATA with Multiple Input Files <bigiain@mightymedia.com.au>
    Re: Using DATA with Multiple Input Files <mshelor@comcast.removeme.net>
    Re: Using DATA with Multiple Input Files <bik.mido@tiscalinet.it>
    Re: Using DATA with Multiple Input Files (Anno Siegel)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Thu, 29 Jan 2004 09:51:00 -0700
From: Ravi Parimi <parimi@nowhere.none.com>
Subject: update file in real time via CGI script
Message-Id: <Pine.GSO.4.58.0401290937140.20086@shelltoe.ece.arizona.edu>

Hi,
	I have a process that is writing to a file and it takes about 5
minutes before the writing is done.

I would like to view the file as it is being updated via a cgi script.

I tried using a pipe as:

open F,"tail -f -n 1000 my_file" or die "$!";

while(<F>) {
	print;
}

This program works fine when I run it from the command line. However when
I run it as a CGI script(with all headers in place), I cannot see
any output, and the browser freezes...

The "-n 1000" args for tail are just to make sure I get all the lines from
the file. Any help is greatly appreciated.


Many thanks,
--ravi



------------------------------

Date: 29 Jan 2004 17:39:19 GMT
From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)
Subject: Re: update file in real time via CGI script
Message-Id: <bvbgg7$9jg$1@canopus.cc.umanitoba.ca>

In article <Pine.GSO.4.58.0401290937140.20086@shelltoe.ece.arizona.edu>,
Ravi Parimi  <parimi@nowhere.none.com> wrote:
:	I have a process that is writing to a file and it takes about 5
:minutes before the writing is done.

:I would like to view the file as it is being updated via a cgi script.

:I tried using a pipe as:

:open F,"tail -f -n 1000 my_file" or die "$!";

You are missing the leading pipe symbol indicating that you are reading
from a different process.

:while(<F>) {
:	print;
:}

:This program works fine when I run it from the command line. However when
:I run it as a CGI script(with all headers in place), I cannot see
:any output, and the browser freezes...

Unless you emitted a <PRE> tag ahead of this, your browser is
waiting for the single long section to finish in order to render it
as HTML. Recall that normally newlines are just whitespace in HTML.

If you want to see the output as it is being generated, you need to
use one of the HTML mechanisms to push out further output. That can
be by periodically closing the HTML and using a meta refresh (in which
case your CGI has to keep track of what to send this time). Another
possibility is to use a chain of 'multipart' content types, with each
new part reflecting additional output. But that's an HTML trick,
not a perl trick. Check around for information on html push technology.
-- 
Aleph sub {Aleph sub null} little, Aleph sub {Aleph sub one} little,
Aleph sub {Aleph sub two} little infinities...


------------------------------

Date: Tue, 27 Jan 2004 02:45:44 GMT
From: yapd <ya_perl_dude@yahoo.com>
Subject: use of stat and argument isn't numeric message
Message-Id: <pan.2004.01.27.02.45.43.803724@yahoo.com>

I saw a particular use of stat in a book. The snippet was:

if (!$stat[4] && !$stat[5] && !$stat[6] && !$stat[7] && !$stat[8]){
    return 0;
}

It would return from a subroutine that was determining if a file was
readable (else it was deemed damaged). I was wondering what, if anything
this particular snippet was checking. I would be grateful if someone
would reveal what I may be missing. 

The entire program as it appeared in the text, for purposes of context,
is below. 

A second issue I was hoping someone could comment on: 
this error occuring on the bounds checking in the for loop: 

"# read the file one byte at a time""

Argument "\x{4a}" isn't numeric in numeric lt (<) at line 61.
Argument "\x{59}" isn't numeric in numeric lt (<) at line 61.
Argument "\x{75}" isn't numeric in numeric lt (<) at line 61.
Argument "\x{6e}" isn't numeric in numeric lt (<) at line 61.
Argument "\x{71}" isn't numeric in numeric lt (<) at line 61.
Argument "\x{6f}" isn't numeric in numeric lt (<) at line 61.
Argument "\x{62}" isn't numeric in numeric lt (<) at line 61.
Argument "\x{6e}" isn't numeric in numeric lt (<) at line 61.
Argument "\x{6f}" isn't numeric in numeric lt (<) at line 61.

I get the message when perl is run with '-w'. However, it is quiet
otherwise. 

Thanks for your comments!



###############

#*
#* search a filesystem "by hand" for damaged files
#*

use Cwd; # module for finding the current working directory
$|=1;    # turn off I/O buffering

sub ScanDirectory {
    my ($workdir) = shift;
    my($startdir) = &cwd; # keep track of where we began

    chdir($workdir) or die "Unable to enter dir $workdir:$!\n";
    opendir(DIR, ".") or die "Unable to open $workdir:$!\n";
    my @names = readdir(DIR);
    closedir(DIR);

    foreach my $name (@names){
        next if ($name eq ".");
        next if ($name eq "..");
        if (-d $name){ # is this a directory?
            &ScanDirectory($name);
            next;
        }
        unless (&CheckFile($name)){
            print &cwd."/".$name."\n"; # print the bad filename
        }
    }

    chdir($startdir) or die "Unable to change to dir $startdir:$!\n";
}

sub CheckFile{
    my($name) = shift;

    print STDERR "Scanning ". &cwd."/".$name."\n";

    # attempt to read the directory entry for this file
    my @stat = stat($name);
    if (!$stat[4] && !$stat[5] && !$stat[6] && !$stat[7] && !$stat[8]){
        return 0;
    }

    # attempt to open this file
    unless (open(T,"$name")){
        return 0;
    }

    # read the file one byte at a time
    for (my $i=0;$i< $stat[7];$i++){
        my $r=sysread(T,$i,1);
        if ($r !=1) {
            close(T);
            return 0;
        }
    }
    close(T);
    return 1;
}

&ScanDirectory(".");



###############
from chapter 2 of ORA's
Perl for System Administration



------------------------------

Date: Tue, 27 Jan 2004 04:37:59 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: use of stat and argument isn't numeric message
Message-Id: <bv4pv7$790$1@wisteria.csv.warwick.ac.uk>


yapd <ya_perl_dude@yahoo.com> wrote:
> A second issue I was hoping someone could comment on: 
> this error occuring on the bounds checking in the for loop: 
> 
> "# read the file one byte at a time""
> 
> Argument "\x{4a}" isn't numeric in numeric lt (<) at line 61.
> Argument "\x{59}" isn't numeric in numeric lt (<) at line 61.
> Argument "\x{75}" isn't numeric in numeric lt (<) at line 61.
> Argument "\x{6e}" isn't numeric in numeric lt (<) at line 61.
> Argument "\x{71}" isn't numeric in numeric lt (<) at line 61.
> Argument "\x{6f}" isn't numeric in numeric lt (<) at line 61.
> Argument "\x{62}" isn't numeric in numeric lt (<) at line 61.
> Argument "\x{6e}" isn't numeric in numeric lt (<) at line 61.
> Argument "\x{6f}" isn't numeric in numeric lt (<) at line 61.
> 
> I get the message when perl is run with '-w'. However, it is quiet
> otherwise. 
> 
<snip>
> 
> #*
> #* search a filesystem "by hand" for damaged files
> #*
> 
> use Cwd; # module for finding the current working directory
> $|=1;    # turn off I/O buffering
> 
> sub ScanDirectory {
>     my ($workdir) = shift;
>     my($startdir) = &cwd; # keep track of where we began

Don't call subs with &. This is perl4ish. Read perldoc perlsub to find
out what effects it has.

>     chdir($workdir) or die "Unable to enter dir $workdir:$!\n";

Hmmm... I wouldn't put "\n" on the end of that. It suppresses useful
information.

>     opendir(DIR, ".") or die "Unable to open $workdir:$!\n";
>     my @names = readdir(DIR);
>     closedir(DIR);
> 
>     foreach my $name (@names){
>         next if ($name eq ".");
>         next if ($name eq "..");
>         if (-d $name){ # is this a directory?
>             &ScanDirectory($name);
>             next;
>         }
>         unless (&CheckFile($name)){
>             print &cwd."/".$name."\n"; # print the bad filename
>         }
>     }
> 
>     chdir($startdir) or die "Unable to change to dir $startdir:$!\n";
> }
> 
> sub CheckFile{
>     my($name) = shift;
> 
>     print STDERR "Scanning ". &cwd."/".$name."\n";
> 
>     # attempt to read the directory entry for this file
>     my @stat = stat($name);
>     if (!$stat[4] && !$stat[5] && !$stat[6] && !$stat[7] && !$stat[8]){

Well, this appears to check for a file user and group root, not a
special file, zero-length and not accessed since the epoch. I have to
say I can't see any reason for singling out this particular class of
file to consider 'bad', but then I'm not an expert on Unix system
administration.

>         return 0;
>     }
> 
>     # attempt to open this file
>     unless (open(T,"$name")){

I would always use a lexical filehandle, and there's no need to quote
$name:

unless (open my $T, $name) {

>         return 0;

You should simply 'return' rather than 'return 0' when you want to
return a false value: this returns undef in scalar context and the
empty list in list context, so it's always false.

>     }
> 
>     # read the file one byte at a time
>     for (my $i=0;$i< $stat[7];$i++){

Bleech.

for (1..$stat[7]) {

You don't actually use the loop counter, so it doesn't matter that
I've both renamed it (to $_) and changed the values by one.

>         my $r=sysread(T,$i,1);

This is a bug. $i is both the loop counter and the variable you are
reading into. You want

my $r = sysread $T, my $tmp, 1;

>         if ($r !=1) {
>             close(T);
>             return 0;
>         }

If you'd've used a lexical FH above, it would close automatically on
scope exit.

>     }

I think I would have written

undef $!;
local $/ = \1;
$! and return while <$T>;
$. == $stat[7] or return;

although that does something slightly different (the reading is done
through a buffer). If I wanted to use sysread, I would probably have
done

1 == sysread $T, my $tmp, 1 or return for 1..$stat[7];

but that is arguably less clear than unwrapping the loop.

>     close(T);
>     return 1;
> }
> 
> &ScanDirectory(".");

> ###############
> from chapter 2 of ORA's
> Perl for System Administration

If this is the general standard of the scripts in that book, throw it
away and get another one.

Ben

-- 
               EAT
               KIDS                                          (...er, whoops...)
               FOR                                             ben@morrow.me.uk
               99p


------------------------------

Date: Tue, 27 Jan 2004 21:00:40 GMT
From: MPBroida <mbroida@fake.domain>
Subject: Re: use of stat and argument isn't numeric message
Message-Id: <4016D178.EF236729@fake.domain>

Ben Morrow wrote:
> 
> >     # read the file one byte at a time
> >     for (my $i=0;$i< $stat[7];$i++){
> 
> Bleech.
> 
> for (1..$stat[7]) {

	But will that construct react to changes in $stat[7] 
	within the loop??  What I mean is: will perl look at
	$stat[7] only on the FIRST time through the loop and
	save that value as the end of the range?  Or will it
	re-evaluate $stat[7] on each pass through the loop??

	With the "expanded" loop format the OP used, I'm sure
	(well, pretty sure) it will re-evaluate $stat[7] on
	each pass.  Is it guaranteed that Perl will re-eval
	the end value with the ".." range format?

		Mike


------------------------------

Date: Tue, 27 Jan 2004 21:26:17 -0000
From: "gnari" <gnari@simnet.is>
Subject: Re: use of stat and argument isn't numeric message
Message-Id: <bv6l7c$tof$1@news.simnet.is>

"MPBroida" <mbroida@fake.domain> wrote in message
news:4016D178.EF236729@fake.domain...
> Ben Morrow wrote:
> >
> > >     # read the file one byte at a time
> > >     for (my $i=0;$i< $stat[7];$i++){
> >
> > Bleech.
> >
> > for (1..$stat[7]) {
>
> But will that construct react to changes in $stat[7]
> within the loop??  What I mean is: will perl look at
> $stat[7] only on the FIRST time through the loop and
> save that value as the end of the range?

yes

>      Or will it
> re-evaluate $stat[7] on each pass through the loop??

no

>
> With the "expanded" loop format the OP used, I'm sure
> (well, pretty sure) it will re-evaluate $stat[7] on
> each pass.

yes

but in this case re-evaluation was not needed


gnari





------------------------------

Date: Wed, 28 Jan 2004 22:35:39 GMT
From: MPBroida <mbroida@fake.domain>
Subject: Re: use of stat and argument isn't numeric message
Message-Id: <4018393B.B4143F75@fake.domain>

gnari wrote:
> 
> "MPBroida" <mbroida@fake.domain> wrote in message
> news:4016D178.EF236729@fake.domain...
> > Ben Morrow wrote:
> > >
> > > >     # read the file one byte at a time
> > > >     for (my $i=0;$i< $stat[7];$i++){
> > >
> > > Bleech.
> > >
> > > for (1..$stat[7]) {
> >
> > But will that construct react to changes in $stat[7]
> > within the loop??  What I mean is: will perl look at
> > $stat[7] only on the FIRST time through the loop and
> > save that value as the end of the range?
> 
> yes
> 
> >      Or will it
> > re-evaluate $stat[7] on each pass through the loop??
> 
> no
> 
> >
> > With the "expanded" loop format the OP used, I'm sure
> > (well, pretty sure) it will re-evaluate $stat[7] on
> > each pass.
> 
> yes
> 
> but in this case re-evaluation was not needed

	OK, so in cases where the end of the range may vary within
	the loop, it would be BAD to use the "start..end" kind of
	if loop.  I'll try to remember that.  :)  In other cases,
	it is definitely much cleaner/simpler to use "..".

		Thanks!
			Mike


------------------------------

Date: Thu, 29 Jan 2004 00:05:44 -0000
From: "gnari" <gnari@simnet.is>
Subject: Re: use of stat and argument isn't numeric message
Message-Id: <bv9iue$a1u$1@news.simnet.is>

> OK, so in cases where the end of the range may vary within
> the loop, it would be BAD to use the "start..end" kind of
> if loop.  I'll try to remember that.  :)  In other cases,
> it is definitely much cleaner/simpler to use "..".

just remember that .. is a list constructor
  for my $i (1..100000) {...}
iterates through a (long) list of elements
the  for (;;) loop is just a funny kind of while loop

gnari





------------------------------

Date: 29 Jan 2004 11:21:49 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: use of stat and argument isn't numeric message
Message-Id: <bvaqcd$s1v$2@mamenchi.zrz.TU-Berlin.DE>

gnari <gnari@simnet.is> wrote in comp.lang.perl.misc:
> > OK, so in cases where the end of the range may vary within
> > the loop, it would be BAD to use the "start..end" kind of
> > if loop.  I'll try to remember that.  :)  In other cases,
> > it is definitely much cleaner/simpler to use "..".
> 
> just remember that .. is a list constructor
>   for my $i (1..100000) {...}
> iterates through a (long) list of elements

But the list isn't expanded.  The compiler is clever enough to generate
the elements one by one at run time.

This hasn't always been so, but the feature was introduced early in
Perl 5.x.

Anno


------------------------------

Date: Wed, 28 Jan 2004 13:05:06 -0800
From: Paul Spitalny <no_spam@no_spam.com>
Subject: using a message box - some questions.
Message-Id: <40182402.6090602@no_spam.com>

Hi,
I want to use the message box. Trouble is, when I call the perl program 
the command window (cmd.exe) also pops-up, in addition to the msg box. 
See code below:

#! /usr/bin/perl
use Win32;
# display a message box with an exclamation mark and an 'Ok' button
sub MsgBox 	{
	my ($caption, $message, $icon_buttons) = @_;
	my @return = qw/- Ok Cancel Abort Retry Ignore Yes No/;
	my $result = Win32::MsgBox($message, $icon_buttons, $caption);
	return $return[$result];
      		}
#$thing = $ARGV[0];
$fred='oh baby';
MsgBox("$thing", "$fred", 1);
# the "1" , above, means OK and cancel buttons


So, my question is, how do I run the perl script without the Windows 
cmd.com window popping up??

Thanks !!

Paul



------------------------------

Date: Wed, 28 Jan 2004 17:21:31 -0500
From: Paul Lalli <ittyspam@yahoo.com>
Subject: Re: using a message box - some questions.
Message-Id: <20040128171929.B29989@dishwasher.cs.rpi.edu>

On Wed, 28 Jan 2004, Paul Spitalny wrote:

> Hi,
> I want to use the message box. Trouble is, when I call the perl program
> the command window (cmd.exe) also pops-up, in addition to the msg box.
> See code below:
>
> #! /usr/bin/perl
> use Win32;
> # display a message box with an exclamation mark and an 'Ok' button
> sub MsgBox 	{
> 	my ($caption, $message, $icon_buttons) = @_;
> 	my @return = qw/- Ok Cancel Abort Retry Ignore Yes No/;
> 	my $result = Win32::MsgBox($message, $icon_buttons, $caption);
> 	return $return[$result];
>       		}
> #$thing = $ARGV[0];
> $fred='oh baby';
> MsgBox("$thing", "$fred", 1);
> # the "1" , above, means OK and cancel buttons
>
>
> So, my question is, how do I run the perl script without the Windows
> cmd.com window popping up??
>

If you're using the Active State distribution, you want to use wperl.exe
instead of perl.exe.   When creating the shortcut to the script, give the
command as "wperl.exe c:\docume~1\user\Desktop\msg.pl"  (or whatever the
path is to your file).  wperl will not display any console window.

Paul Lalli


------------------------------

Date: Wed, 28 Jan 2004 15:28:09 -0800
From: Paul Spitalny <no_spam@no_spam.com>
Subject: Re: using a message box - some questions.
Message-Id: <40184589.9000106@no_spam.com>

Paul Lalli wrote:
> On Wed, 28 Jan 2004, Paul Spitalny wrote:
> 
> 
>>Hi,
>>I want to use the message box. Trouble is, when I call the perl program
>>the command window (cmd.exe) also pops-up, in addition to the msg box.
>>See code below:
>>
>>#! /usr/bin/perl
>>use Win32;
>># display a message box with an exclamation mark and an 'Ok' button
>>sub MsgBox 	{
>>	my ($caption, $message, $icon_buttons) = @_;
>>	my @return = qw/- Ok Cancel Abort Retry Ignore Yes No/;
>>	my $result = Win32::MsgBox($message, $icon_buttons, $caption);
>>	return $return[$result];
>>      		}
>>#$thing = $ARGV[0];
>>$fred='oh baby';
>>MsgBox("$thing", "$fred", 1);
>># the "1" , above, means OK and cancel buttons
>>
>>
>>So, my question is, how do I run the perl script without the Windows
>>cmd.com window popping up??
>>
> 
> 
> If you're using the Active State distribution, you want to use wperl.exe
> instead of perl.exe.   When creating the shortcut to the script, give the
> command as "wperl.exe c:\docume~1\user\Desktop\msg.pl"  (or whatever the
> path is to your file).  wperl will not display any console window.
> 
> Paul Lalli

Hi Paul,
Thanks a lot, that did the trick !!

By the way, what exactly IS wperl as compared to PERL ??

Paul





------------------------------

Date: 28 Jan 2004 23:30:34 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude>
Subject: Re: using a message box - some questions.
Message-Id: <Xns947EBC4A59021asu1cornelledu@132.236.56.8>

Paul Spitalny <no_spam@no_spam.com> wrote in
news:40184589.9000106@no_spam.com: 

> Paul Lalli wrote:
>
> By the way, what exactly IS wperl as compared to PERL ??

It is perl not, PERL. wperl is a Windows specific tool that does the job of 
running your script without creating a console window.

-- 
A. Sinan Unur
1usa@llenroc.ude (reverse each component for email address)


------------------------------

Date: Wed, 28 Jan 2004 23:52:21 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: using a message box - some questions.
Message-Id: <VWXRb.1755$VY1.1312@nwrddc02.gnilink.net>

Paul Spitalny wrote:
> By the way, what exactly IS wperl as compared to PERL ??

I don't know about wperl.
Are you refering to wperl.exe, maybe? This has been discussed a few times
before. As far as I remember there only one byte different between perl.exe
and wperl.exe. This byte tells Windows to run the program without launching
a command window.

Now, I don't know what PERL is either.
Are you are refering to perl, maybe? That would be the Perl interpreter.
Or are you refering to Perl, maybe? That would be the Perl programming
language.

So I really don't know how to compare something that I can't quite identify
(wperl) with something that doesn't even exit (PERL).

jue




------------------------------

Date: Thu, 29 Jan 2004 09:32:38 -0800
From: Paul Spitalny <no_spam@no_spam.com>
Subject: Re: using a message box - some questions.
Message-Id: <101igtqh0oul6a@corp.supernews.com>

Jürgen Exner wrote:
> Paul Spitalny wrote:
> 
>>By the way, what exactly IS wperl as compared to PERL ??
> 
> 
> I don't know about wperl.
> Are you refering to wperl.exe, maybe? This has been discussed a few times
> before. As far as I remember there only one byte different between perl.exe
> and wperl.exe. This byte tells Windows to run the program without launching
> a command window.
> 
> Now, I don't know what PERL is either.
> Are you are refering to perl, maybe? That would be the Perl interpreter.
> Or are you refering to Perl, maybe? That would be the Perl programming
> language.
> 
> So I really don't know how to compare something that I can't quite identify
> (wperl) with something that doesn't even exit (PERL).
> 
> jue
> 
> 
Hi Jue, and A. Sinan Unur,
Yes, I meant wperl.exe and I also meant perl.exe.

So, now, I understand that wperl.exe is the same as perl.exe except that 
the program does not launch a command window. And that is just what I 
wanted!

Thanks,

Paul


------------------------------

Date: Tue, 27 Jan 2004 18:43:25 -0700
From: Mark Shelor <mshelor@comcast.removeme.net>
Subject: Using DATA with Multiple Input Files
Message-Id: <VsydnXn3G94ijordRVn-uw@comcast.com>

Here's a problem I've encountered more than once, and didn't have a 
clean solution:

Suppose I have multiple input files, and would like to make their 
contents part of the script, rather than having the script access them 
as external files.  If I have one input file, this is easy to do by 
means of the __DATA__ token.

Can something similar be done with multiple input files?

TIA, Mark



------------------------------

Date: Wed, 28 Jan 2004 12:54:06 +1100
From: Iain Chalmers <bigiain@mightymedia.com.au>
Subject: Re: Using DATA with Multiple Input Files
Message-Id: <bigiain-FFE214.12540628012004@news.fu-berlin.de>

In article <VsydnXn3G94ijordRVn-uw@comcast.com>,
 Mark Shelor <mshelor@comcast.removeme.net> wrote:

> Here's a problem I've encountered more than once, and didn't have a 
> clean solution:
> 
> Suppose I have multiple input files, and would like to make their 
> contents part of the script, rather than having the script access them 
> as external files.  If I have one input file, this is easy to do by 
> means of the __DATA__ token.
> 
> Can something similar be done with multiple input files?

http://search.cpan.org/~dconway/Inline-Files-0.62/lib/Inline/Files.pm

Damian has solved your problem already :-)

big

-- 
'When I first met Katho, she had a meat cleaver in one hand and
half a sheep in the other. "Come in", she says, "Hammo's not here.
I hope you like meat.' Sharkey in aus.moto


------------------------------

Date: Tue, 27 Jan 2004 22:27:25 -0700
From: Mark Shelor <mshelor@comcast.removeme.net>
Subject: Re: Using DATA with Multiple Input Files
Message-Id: <zoidnU3yFsTd1YrdRVn-uQ@comcast.com>

Iain Chalmers wrote:
> In article <VsydnXn3G94ijordRVn-uw@comcast.com>,
>  Mark Shelor <mshelor@comcast.removeme.net> wrote:
>>
>>Suppose I have multiple input files, and would like to make their 
>>contents part of the script, rather than having the script access them 
>>as external files.  If I have one input file, this is easy to do by 
>>means of the __DATA__ token.
>>
>>Can something similar be done with multiple input files?
> 
> http://search.cpan.org/~dconway/Inline-Files-0.62/lib/Inline/Files.pm
> 
> Damian has solved your problem already :-)


Thanks Iain.  It looks like he has.  I'm a bit concerned by the warnings 
in the docs, however:

"WARNING

"It is possible that this module may overwrite the source code in files 
that use it. To protect yourself against this possibility, you are 
strongly advised to use the -backup option described in "Safety first".

"This module is still experimental. Regardless of whether you use 
-backup or not, by using this module you agree that the authors will 
b<under no circumstances> be responsible for any loss of data, code, 
time, money, or limbs, or for any other disadvantage incurred as a 
result of using Inline::Files."

Yikes!

Mark



------------------------------

Date: Thu, 29 Jan 2004 16:10:46 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: Using DATA with Multiple Input Files
Message-Id: <p74i10lhtaks8oqadlsst5l8182o2epld7@4ax.com>

On Tue, 27 Jan 2004 18:43:25 -0700, Mark Shelor
<mshelor@comcast.removeme.net> wrote:

>Suppose I have multiple input files, and would like to make their 
>contents part of the script, rather than having the script access them 
>as external files.  If I have one input file, this is easy to do by 
>means of the __DATA__ token.
>
>Can something similar be done with multiple input files?

A poster already pointed you to a relevant module. However, as usual
in Perl, TIMTOWTDI and since we don't know what are your real
requisites, we can't suggest which one is the best, provided such a
"best" exists. For example you could use HERE documents just as easily
as the DATA FH, but I really don't know...

In any case, since your files are small enough that you would find it
handy to put their contents into your code, it is reasonable to guess
it wouldn't be prohibitive to hold them in memory, would it?

Just see the following self-explanatory oversimplified example, maybe
you can adapt it to your needs...

  #!/usr/bin/perl
  
  use strict;
  use warnings;
  
  my @fh;
  
  {
      local $/=''; my $i;
      open $fh[$i++], '<', \$_ for <DATA>;
  }
  
  print scalar <$_> for @fh;
  
  __END__
  aaa
  bbb
  ccc
  
  un
  due
  tre
  
  foo
  bar
  baz

OTOH, if you want to readline() from one of those FHs with the <>
operator, you can't do that directly, so you may want to play with the
symbol table:

  #!/usr/bin/perl
  
  use strict;
  use warnings;
  
  my @fh;
  
  {
      local $/=''; my $i;
      open $fh[$i++], '<', \$_ for <DATA>;
      no strict 'refs';
      *{"DATA$_"}=$fh[$_] for 0..$#fh;
  }
  
  print scalar <$_> for @fh;
  print "---\n", <DATA1>;
  
  __END__
  ...

Be warned though that the usual cmts wrt the use of symrefs apply, so
in doubt, please ask here and most importantly, *please*, do *not*
abuse this technique...


HTH,
Michele
-- 
you'll see that it shouldn't be so. AND, the writting as usuall is
fantastic incompetent. To illustrate, i quote:
- Xah Lee trolling on clpmisc,
  "perl bug File::Basename and Perl's nature"


------------------------------

Date: 28 Jan 2004 16:31:33 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Using DATA with Multiple Input Files
Message-Id: <bv8o55$hsg$2@mamenchi.zrz.TU-Berlin.DE>

Mark Shelor  <mshelor@comcast.removeme.net> wrote in comp.lang.perl.misc:
> Iain Chalmers wrote:
> > In article <VsydnXn3G94ijordRVn-uw@comcast.com>,
> >  Mark Shelor <mshelor@comcast.removeme.net> wrote:
> >>
> >>Suppose I have multiple input files, and would like to make their 
> >>contents part of the script, rather than having the script access them 

[...]

> > Damian has solved your problem already :-)

[...]

> Thanks Iain.  It looks like he has.  I'm a bit concerned by the warnings 
> in the docs, however:
> 
> "WARNING
> 
> "It is possible that this module may overwrite the source code in files 
> that use it. To protect yourself against this possibility, you are 
> strongly advised to use the -backup option described in "Safety first".
> 
> "This module is still experimental. Regardless of whether you use 
> -backup or not, by using this module you agree that the authors will 
> b<under no circumstances> be responsible for any loss of data, code, 
> time, money, or limbs, or for any other disadvantage incurred as a 
> result of using Inline::Files."
> 
> Yikes!

Well, just from the module's function it must modify, i.e. overwrite, the
user's source.  As a module author, you *must* cover your ass if you do
that.  If some clown blames anything that happened to their source to the
module, you'd have a hard time proving you're innocent.

Damian is a more-than-competent programmer, the module won't mess up
anything.  Just don't assign your backup to /dev/null :)

Anno


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 6049
***************************************


home help back first fref pref prev next nref lref last post