[15445] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2855 Volume: 9

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Apr 24 18:10:32 2000

Date: Mon, 24 Apr 2000 15:10:20 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <956614219-v9-i2855@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Mon, 24 Apr 2000     Volume: 9 Number: 2855

Today's topics:
        newbie: How many characters in each row? (Doran)
    Re: newbie: How many characters in each row? <matt@starkmedia.com>
    Re: newbie: How many characters in each row? (Doran)
    Re: newbie: How many characters in each row? <matt@starkmedia.com>
    Re: newbie: How many characters in each row? <matt@starkmedia.com>
        Oracle DBI DBM memory leak taric@purpletunic.com
        Out of memory! vinman72@my-deja.com
        Parsing lines of a file? <mflaherty2@earthlink.net>
    Re: Parsing lines of a file? <DNess@Home.Com>
    Re: Parsing lines of a file? <makarand_kulkarni@My-Deja.com>
    Re: Parsing lines of a file? <lr@hpl.hp.com>
    Re: Parsing lines of a file? <godzilla@stomp.stomp.tokyo>
        perl preprocessor <callahab@my-deja.com>
    Re: perl preprocessor (Greg Bacon)
    Re: Perl vs Java <gellyfish@gellyfish.com>
    Re: perlcc: No dbm on this machine? <gellyfish@gellyfish.com>
        Problem with getting return value from a function call <stingray@davesworld.net>
        REGEX tutorials ? <vette_2000@my-deja.com>
    Re: REGEX tutorials ? (Bart Lateur)
        Scoping trouble with my $foo <ver@sims.nl>
    Re: Scoping trouble with my $foo <tony_curtis32@yahoo.com>
    Re: sendmail problem in the perl script <stassenm@my-deja.com>
    Re: Show me if you don't mind <gellyfish@gellyfish.com>
    Re: socket fd exit with child? <rootbeer@redcat.com>
        store hex in db, and retrieve as hex <hmerrill@my-deja.com>
    Re: store hex in db, and retrieve as hex (Greg Bacon)
    Re: to print a flat data file with a certain lengths ? <gellyfish@gellyfish.com>
    Re: to print a flat data file with a certain lengths ? <makarand_kulkarni@My-Deja.com>
    Re: URGENT help needed <gellyfish@gellyfish.com>
        Using File::stat <loo02@attglobal.net>
    Re: Using File::stat <tony_curtis32@yahoo.com>
    Re: Using File::stat <loo02@attglobal.net>
    Re: Using File::stat <DNess@Home.Com>
    Re: Which book? <DNess@Home.Com>
        Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)

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

Date: Mon, 24 Apr 2000 19:08:09 GMT
From: doran@NOSPAMaltx.net (Doran)
Subject: newbie: How many characters in each row?
Message-Id: <390499b6.4833233@news2.brandx.net>

Is there a rule (either a "real" rule or a suggested style rule)
regarding how many characters can be in each line of code? I try to
limit it to about 72 characters/line, but (particularly when there are
a number of tabs an/or HTML code) that is often difficult to achieve
without spending a lot of extra time prettying things up. Is there a
problem with having a line that's, say 120 (or more) characters long?

I'm interested in both "real" problems (it breaks the code) and
"style" problems (it makes it difficult to read), but primarily the
former.

TIA
Doran...




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

Date: Mon, 24 Apr 2000 14:18:12 -0500
From: Matthew Leonhardt <matt@starkmedia.com>
Subject: Re: newbie: How many characters in each row?
Message-Id: <39049DF4.86DB8E3B@starkmedia.com>

Doran wrote:
> 
> Is there a rule (either a "real" rule or a suggested style rule)
> regarding how many characters can be in each line of code? I try to
> limit it to about 72 characters/line, but (particularly when there are
> a number of tabs an/or HTML code) that is often difficult to achieve
> without spending a lot of extra time prettying things up. Is there a
> problem with having a line that's, say 120 (or more) characters long?
> 
> I'm interested in both "real" problems (it breaks the code) and
> "style" problems (it makes it difficult to read), but primarily the
> former.

As long as your editor doesn't do you the "favor" of inserting newlines
for you when your lines get too long, it won't break your code.  There's
really no excuse for sloppy, long code, though.  If you need to print a
tab delimited group of scalars, I would use one of these options:

print "$a\t$b\t$c\t$d\n"; # or
print "$a	$b	"
    . "$c	$d\n";

It's much more readable than:

print "$a	$b	$c	$d\n";

When dealing with large amounts of tabs, or longer scalar names.  There
are rare exceptions I usually make such as:

while (my ($user, $pass, $first, $last, $addr1, $addr2, $etc) =
$sqh->fetchrow_array) {
	# do something
}

It all depends on personal style and what the people who need to read
your code will tolerate.  I use to work with a guy who'd write code like
this:

use CGI;
$query=new CGI;
if($query->param('bob') eq 'mary'){
print "Content-type:text/html

<html><head><title>Some Title Here</title></head><body bgcolor="white">
<B>Hi Bob</b>
";
}else{
print "Content-type:text/html

<body>Hey, you're not bob!";
}

Needless to say, he didn't hang around long ;)

Matt

--
Matthew Leonhardt
Programmer/Analyst

Starkmedia | for the web
219 N. Milwaukee Street
Milwaukee, WI  53202

p 414.226.2710
f 414.226.2705
e matt@starkmedia.com
w http://www.starkmedia.com


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

Date: Mon, 24 Apr 2000 19:47:50 GMT
From: doran@NOSPAMaltx.net (Doran)
Subject: Re: newbie: How many characters in each row?
Message-Id: <3904a1fa.6949756@news2.brandx.net>

>really no excuse for sloppy, long code, though.  If you need to print a
>tab delimited group of scalars, I would use one of these options:
>
>print "$a\t$b\t$c\t$d\n"; # or
>print "$a	$b	"
>    . "$c	$d\n";

Thanks for the tips. For the sake of clarity, I need to mention that I
actually meant "indentation" when I was referring to tabs.

sub some_code{
	open FILE, $somefile;
	while (<FILE>){
		if (/something/){
			print "Indentation has used up 12 spaces\n";
		}
	}
	close FILE;
}

It's less of a hassle with straight perl code than with HTML, where
tags, embedded w/urls and parameters can easily add up to a hundred
characters.

Thanks again,
Doran...




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

Date: Mon, 24 Apr 2000 14:54:03 -0500
From: Matthew Leonhardt <matt@starkmedia.com>
Subject: Re: newbie: How many characters in each row?
Message-Id: <3904A65B.3FA30F69@starkmedia.com>

Doran wrote:
> 
> >really no excuse for sloppy, long code, though.  If you need to print a
> >tab delimited group of scalars, I would use one of these options:
> >
> >print "$a\t$b\t$c\t$d\n"; # or
> >print "$a      $b      "
> >    . "$c      $d\n";
> 
> Thanks for the tips. For the sake of clarity, I need to mention that I
> actually meant "indentation" when I was referring to tabs.
> 
> sub some_code{
>         open FILE, $somefile;
>         while (<FILE>){
>                 if (/something/){
>                         print "Indentation has used up 12 spaces\n";
>                 }
>         }
>         close FILE;
> }
> 
> It's less of a hassle with straight perl code than with HTML, where
> tags, embedded w/urls and parameters can easily add up to a hundred
> characters.

I really think that's a matter of personal preference.  I always use
tabs to indent.  I know some people who use two or three spaces.  Most
programmers I know are using IDE's that look better with tabs.

--
Matthew Leonhardt
Programmer/Analyst

Starkmedia | for the web
219 N. Milwaukee Street
Milwaukee, WI  53202

p 414.226.2710
f 414.226.2705
e matt@starkmedia.com
w http://www.starkmedia.com


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

Date: Mon, 24 Apr 2000 14:54:12 -0500
From: Matthew Leonhardt <matt@starkmedia.com>
Subject: Re: newbie: How many characters in each row?
Message-Id: <3904A664.F7D18865@starkmedia.com>

Doran wrote:
> 
> >really no excuse for sloppy, long code, though.  If you need to print a
> >tab delimited group of scalars, I would use one of these options:
> >
> >print "$a\t$b\t$c\t$d\n"; # or
> >print "$a      $b      "
> >    . "$c      $d\n";
> 
> Thanks for the tips. For the sake of clarity, I need to mention that I
> actually meant "indentation" when I was referring to tabs.
> 
> sub some_code{
>         open FILE, $somefile;
>         while (<FILE>){
>                 if (/something/){
>                         print "Indentation has used up 12 spaces\n";
>                 }
>         }
>         close FILE;
> }
> 
> It's less of a hassle with straight perl code than with HTML, where
> tags, embedded w/urls and parameters can easily add up to a hundred
> characters.

I really think that's a matter of personal preference.  I always use
tabs to indent.  I know some people who use two or three spaces.  Most
programmers I know are using IDE's that look better with tabs.

--
Matthew Leonhardt
Programmer/Analyst

Starkmedia | for the web
219 N. Milwaukee Street
Milwaukee, WI  53202

p 414.226.2710
f 414.226.2705
e matt@starkmedia.com
w http://www.starkmedia.com


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

Date: Mon, 24 Apr 2000 19:27:04 GMT
From: taric@purpletunic.com
Subject: Oracle DBI DBM memory leak
Message-Id: <8e275q$5qo$1@nnrp1.deja.com>

Hi.  I may have found a memory leak in DBI or DBM:Oracle,
has anyone else run into this?

My system is: DEC Alpha,  Perl version 5.004_04, and
Oracle 8.0.5.

I wrote a program to test and illustrate the problem.  It is
just a continuous loop that connects and disconnects with Oracle.
Memory use appears to start at 23M and then increase about 1M every
5 seconds.  Eventually it crashes the machine of course.

I am hoping to upgrade to Oracle 8i and the latest version of Perl
early next year, perhaps that will fix it.

Thanks!

-Taric
 taric@purpletunic.com



#!/usr/bin/perl -w

use DBI;
use strict;

while(1)
{
   connect_and_disconnect();
}

sub connect_and_disconnect()
{
   my $dbh;

   my %StdAttr = (
       PrintError => 1,
       AutoCommit => 0,
       RaiseError => 1
       );

    eval {
        $dbh = DBI->connect('DBI:Oracle:dbname','user/password',
                            "", \%StdAttr);
    };
    if ($@) {
      print $@;
      exit 1;
    }

   if (!$dbh) { print "Error"; exit 1; }
   $dbh->disconnect;
   undef $dbh;
}


Sent via Deja.com http://www.deja.com/
Before you buy.


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

Date: Mon, 24 Apr 2000 21:14:31 GMT
From: vinman72@my-deja.com
Subject: Out of memory!
Message-Id: <8e2dff$c1o$1@nnrp1.deja.com>

Having a big problem with this since it's incredibly frustrating! I get
an "Out of memory!" by simply reading in a 35MB file into a variable.
I'm running on a UNIX RS6000 platform with 1GB of memory using Perl5.
Here's the scenario of the loop in pseudocode:
           1) Initialize variables to null
           2) I read in one file (35MB) into a variable
           3) I do some parsing near the end of the file
           4) I append (or create if non-existing) to another file
           5) Process next file

I know that I get the out of memory message while reading it into a
variable via the debugger. I get no warnings despite using the -w
switch.

I get the "Out of memory!" not on the first 35MB file but on the second
one (91), as is seen in the following actual code:

open (OUTFILE, ">/collector/test.ver");

undef $/;

foreach $cn (90,91) {
$a = 0;
$contents = \0;
$newcontents = \0;

$a = open(COLL, "/ums/collector/collector$cn/d20000419235804c$cn.ver" );
print "Open file of $cn returned: $a \n";

binmode <COLL>;
$contents = <COLL>;

$trailerpos = rindex($contents, "~~~~~~~~");
$trailerpos -= 14;

$newcontents = substr($contents, 0, $trailerpos);
print OUTFILE $newcontents;

close COLL;
}

Interestingly this method does work with files > 35MB. However, at
times, it fails on files even as small as 5MB. Data related? Any
suggestions, corrections or info I should be aware of? Please help.
Thanks.


Sent via Deja.com http://www.deja.com/
Before you buy.


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

Date: Mon, 24 Apr 2000 18:47:43 GMT
From: "Mike Flaherty" <mflaherty2@earthlink.net>
Subject: Parsing lines of a file?
Message-Id: <jF0N4.81459$q67.1491007@newsread2.prod.itd.earthlink.net>

Hello All,

This must be basic stuff but I can't find a straight forward way to do this.
All I want to do is read the file L.L line by line.  As I do, I would like
to write only fields 0,1,2,3, of each line to an output file.  As you can
see, I am able to read in each line but when I try to assign $line into an
array so that I can print @line[0..3], I get nothing.  What am I missing,

Thanks as Always,
Mike


$ cat extract2.pl
#!/usr/local/bin/perl

open(tideraw,"l.l");
while($line=<tideraw>)
{
 print $line;
}
close (tideraw);

$ ./extract2.pl
2000-04-24  3:39 AM EDT   9.69 feet  High Tide
2000-04-24  4:19 PM EDT   8.65 feet  High Tide
2000-04-25  4:27 AM EDT   9.35 feet  High Tide
2000-04-25  5:10 PM EDT   8.39 feet  High Tide
2000-04-26  5:20 AM EDT   9.08 feet  High Tide





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

Date: Mon, 24 Apr 2000 18:56:33 GMT
From: David Ness <DNess@Home.Com>
Subject: Re: Parsing lines of a file?
Message-Id: <390498E6.FD6DB368@Home.Com>

Mike Flaherty wrote:
> 
> Hello All,
> 
> This must be basic stuff but I can't find a straight forward way to do this.
> All I want to do is read the file L.L line by line.  As I do, I would like
> to write only fields 0,1,2,3, of each line to an output file.  As you can
> see, I am able to read in each line but when I try to assign $line into an
> array so that I can print @line[0..3], I get nothing.  What am I missing,
> 
> Thanks as Always,
> Mike
> 
> $ cat extract2.pl
> #!/usr/local/bin/perl
> 
> open(tideraw,"l.l");
 ...
[snip]

Your program introduces no concept of `field' and $line is not an array.
That may at least be the start of your problems...


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

Date: Mon, 24 Apr 2000 12:03:31 -0700
From: Makarand Kulkarni <makarand_kulkarni@My-Deja.com>
Subject: Re: Parsing lines of a file?
Message-Id: <39049A83.A6D61918@My-Deja.com>

> This must be basic stuff but I can't find a straight forward way to do this.
> All I want to do is read the file L.L line by line.  As I do, I would like
> to write only fields 0,1,2,3, of each line to an output file.  As you can
> see, I am able to read in each line but when I try to assign $line into an
> array so that I can print @line[0..3], I get nothing.  What am I missing,
>

Use a good field separator to separate your fields on each line.
Then use split () to split each line into its fields and store them in
@line. Then you can take a splice to print.
--



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

Date: Mon, 24 Apr 2000 12:13:15 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: Parsing lines of a file?
Message-Id: <MPG.136e3ca5632ebfe798a96d@nntp.hpl.hp.com>

In article <jF0N4.81459$q67.1491007@newsread2.prod.itd.earthlink.net> on 
Mon, 24 Apr 2000 18:47:43 GMT, Mike Flaherty <mflaherty2@earthlink.net> 
says...
> This must be basic stuff but I can't find a straight forward way to do this.
> All I want to do is read the file L.L line by line.  As I do, I would like
> to write only fields 0,1,2,3, of each line to an output file.  As you can
> see, I am able to read in each line but when I try to assign $line into an
> array so that I can print @line[0..3], I get nothing.  What am I missing,

You are missing:

    the '-w flag

    use strict;

    A diagnostic, including $!, on failure to open the file.

    The warning that filehandles shouldn't be lower-case.

    The code that you have tried, which is intended to
    *split* the input line into fields.

    A specification of the fields -- delimiter, columns, ...?

> $ cat extract2.pl
> #!/usr/local/bin/perl
> 
> open(tideraw,"l.l");
> while($line=<tideraw>)
> {
>  print $line;
> }
> close (tideraw);
> 
> $ ./extract2.pl
> 2000-04-24  3:39 AM EDT   9.69 feet  High Tide
> 2000-04-24  4:19 PM EDT   8.65 feet  High Tide
> 2000-04-25  4:27 AM EDT   9.35 feet  High Tide
> 2000-04-25  5:10 PM EDT   8.39 feet  High Tide
> 2000-04-26  5:20 AM EDT   9.08 feet  High Tide

This is really basic, as you say.  Let Perl help you as much as 
possible.  Ask again if you have done the above and have hit another 
barrier.

-- 
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: Mon, 24 Apr 2000 13:09:42 -0700
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: Parsing lines of a file?
Message-Id: <3904AA06.B10B9FFE@stomp.stomp.tokyo>

Mike Flaherty wrote:

> This must be basic stuff but I can't find a 
> straight forward way to do this. All I want 
> to do is read the file L.L line by line.  
> As I do, I would like to write only fields 
> 0,1,2,3, of each line to an output file.  
> As you can see, I am able to read in each 
> line but when I try to assign $line into an
> array so that I can print @line[0..3], 
> I get nothing.  What am I missing,

(snipped)

You have asked for a straight forward
way to accomplish this. Here is a simple
script example you may study, perhaps adapt
and build upon to fit your needs, if you
feel this will help you.

This script prints to your monitor via
our internet, but can be adapted to 
print to a data file quite easily.

What is truly important is to develop
a way to collect your data in a format
which can easily be manipulated. I do
not know exactly what mechanisms you
are using to collect and format your
data. However, take a close look at
how you accomplish this. A managable
data base is very important. Extracting
your data is usually the easy part. You
may wish to focus more on how you are
collecting your data for use.


Double Space example leaves the new line \n
character at the end of each line in place
and adds one more during print.

Single Space example simply leaves the
\n in place and prints.

Single LINE example chomps off the
\n and prints as one long line
with a ¦ printed to delimit for
easier reading or for splitting
into an array later if needed,
in a logical easy-to-see manner.


Godzilla!


========================

Example simple basic code:



#!/usr/local/bin/perl

print "Content-Type: text/plain\n\n";

open (TIDE, "tide.txt");
@Tide = <TIDE>;
close (TIDE);

print "Double Space Example:\n\n";

foreach $tide_line (@Tide)
 { print "$tide_line\n"; }


print "\n\nSingle Space Example:\n\n";

foreach $tide_line (@Tide)
 { print "$tide_line"; }


print "\n\nSingle Line Example:\n\n";

foreach $tide_line (@Tide)
 { 
  chomp ($tide_line);
  print "$tide_line ¦ ";
 }


exit;


========================

These are the contents of tide.txt :


2000-04-24  3:39 AM EDT   9.69 feet  High Tide
2000-04-24  4:19 PM EDT   8.65 feet  High Tide
2000-04-25  4:27 AM EDT   9.35 feet  High Tide
2000-04-25  5:10 PM EDT   8.39 feet  High Tide
2000-04-26  5:20 AM EDT   9.08 feet  High Tide


========================

These are the printed results:


Double Space Example:

2000-04-24  3:39 AM EDT   9.69 feet  High Tide

2000-04-24  4:19 PM EDT   8.65 feet  High Tide

2000-04-25  4:27 AM EDT   9.35 feet  High Tide

2000-04-25  5:10 PM EDT   8.39 feet  High Tide

2000-04-26  5:20 AM EDT   9.08 feet  High Tide



Single Space Example:

2000-04-24  3:39 AM EDT   9.69 feet  High Tide
2000-04-24  4:19 PM EDT   8.65 feet  High Tide
2000-04-25  4:27 AM EDT   9.35 feet  High Tide
2000-04-25  5:10 PM EDT   8.39 feet  High Tide
2000-04-26  5:20 AM EDT   9.08 feet  High Tide


Single Line Example:

2000-04-24  3:39 AM EDT   9.69 feet  High Tide ¦ 2000-04-24  (snipped)


I snipped off the single line to prevent word wrapping.
Its format is:

data ¦ data ¦ data ¦ data ¦ data ¦

Note their is a ¦ at the end which isn't
needed for an array split later.



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

Date: Mon, 24 Apr 2000 18:42:21 GMT
From: Barry Callahan <callahab@my-deja.com>
Subject: perl preprocessor
Message-Id: <8e24ib$2v4$1@nnrp1.deja.com>

I was wondering if there was a perl equivalent to the c preprocessor.

I'd like to be able to do something like:

#if ($^O eq "MSWin32") require "Windows_Setup.pl"

Thanks.

Barry


Sent via Deja.com http://www.deja.com/
Before you buy.


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

Date: 24 Apr 2000 19:07:02 GMT
From: gbacon@ruby.itsc.uah.edu (Greg Bacon)
Subject: Re: perl preprocessor
Message-Id: <8e260m$dmk$2@info2.uah.edu>

In article <8e24ib$2v4$1@nnrp1.deja.com>,
	Barry Callahan <callahab@my-deja.com> writes:

: I was wondering if there was a perl equivalent to the c preprocessor.

You can run your source through the C preprocessor.  Read the perlrun
manpage and search for -P.

: I'd like to be able to do something like:
: 
: #if ($^O eq "MSWin32") require "Windows_Setup.pl"

Why not uncomment it?  Better still, create a dispatch table:

    my %setup = (
        linux   => \&setup_linux_bindings,
        irix    => \&setup_irix_bindings,
        mswin32 => \&setup_win32_bindings,
        ...
    );

    $setup{$^O}->();

Maybe you could get away with something simpler:

    my @setup = ( \&setup_win32_bindings, \&setup_unix_bindings );

    $setup[ $^O eq 'MSWin32' ? 0 : 1 ]->();

You might want to wrap this setup procedure in a BEGIN{} block.

Hope this helps,
Greg
-- 
Arrogance is bliss.
    -- Elizabeth L. Kaminsky


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

Date: 24 Apr 2000 09:33:55 +0100
From: Jonathan Stowe <gellyfish@gellyfish.com>
Subject: Re: Perl vs Java
Message-Id: <8e10tj$ic$1@orpheus.gellyfish.com>

On Sun, 23 Apr 2000 20:14:46 -0400 Kevin Bass wrote:
> Tom Phoenix <rootbeer@redcat.com> wrote in message
> news:Pine.GSO.4.10.10004231601000.25963-100000@user2.teleport.com...
>> On Sun, 23 Apr 2000, Kevin Bass wrote:
>>
>> > What are the advantages and/or disadvantage of using Perl as opposed
>> > to Java?
>>
>> Please use a Usenet archive (such as Deja) to see what has been said when
>> this has come up in the past.
>>
>
> Thanks.  I found alot of information on this topic using www.deja.com.
> 

Phew! Just ignore anything that might be said by one George Reese on the
subject ;-}

/J\
-- 
It's not easy to juggle a pregnant wife and a troubled child, but somehow
I managed to fit in eight hours of TV a day.
-- 
fortune oscar homer


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

Date: 24 Apr 2000 18:16:37 +0100
From: Jonathan Stowe <gellyfish@gellyfish.com>
Subject: Re: perlcc: No dbm on this machine?
Message-Id: <8e1vhl$339$1@orpheus.gellyfish.com>

On Mon, 24 Apr 2000 22:49:28 +0800 Calvin wrote:
>> Stop trying to compile your Perl source. Cheers!
> 
> Oh, sorry. Why shouldn't we compile the perl? Would anyone explain more on
> this issue?
> 

From the perlcc manpage :

BUGS
       The whole compiler suite (`perlcc' included) should be
       considered very experimental.  Use for production purposes
       is strongly discouraged.

       perlcc currently cannot compile shared objects on Win32.
       This should be fixed in future.

       Bugs in the various compiler backends still exist, and are
       perhaps too numerous to list here.

/J\
-- 
The strong must protect the sweet
-- 
fortune oscar homer


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

Date: Mon, 24 Apr 2000 13:49:21 -0500
From: Chris Lacey <stingray@davesworld.net>
Subject: Problem with getting return value from a function call
Message-Id: <39049731.B86E7AB1@davesworld.net>

Hello,

I have been working with some logic where I call a function within a
class if a file exists.  I ask the user to indicate whether or not they
want to overwrite the file and return the answer for evaluation in the
if statement below.

I am having problems getting a value returned from the function.  I have
read the OO modules in O'reilly CD bookshelf and various posts, but
still have not been able to find out what I am doing wrong.  Could
someone please look at this code and provide some guidance please?
Thanks,

Chris Lacey

          SafeWrite::okToWriteTest($name);
           if (\$choice eq "Y" or "y") {
                open (sortedUserOut, ">sortedUserOut.txt")
                 or die "Error writing sortedUserOut file... : $!";


************* called function follows *******************

sub okToWriteTest {

 # initialize choice variable
my $choice = "n";

 # Receive parm object which was passed to this function and store it in
$arg
 my $arg = shift;

print "---------------- W A R N I N G ------------------------\n\n";
print "   I see you already have a file named $arg           \n";
print " Do you want to overwrite it with an updated version?  \n\n";
print "                 Enter Y or N please...                \n\n";
print " A choice of N will stop program so you can make a copy.\n";
print "-------------------------------------------------------\n";
print "\a";               # Sounds a beep to get attention
$choice = <STDIN>;
chomp ($choice);
return \$choice;          # return a reference to the local variable
$choice
}



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

Date: Mon, 24 Apr 2000 19:14:24 GMT
From: Stumpy <vette_2000@my-deja.com>
Subject: REGEX tutorials ?
Message-Id: <8e26e6$4rv$1@nnrp1.deja.com>

I'm aware there are many Perl tutorials available on the www.  And that
most of these tutorials cover REGEX to some degree or another.

Is there one or two the members of this newsgroup consider better
coverage / explanation of REGEX than the others ?

Thanks,
Mike


Sent via Deja.com http://www.deja.com/
Before you buy.


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

Date: Mon, 24 Apr 2000 21:00:30 GMT
From: bart.lateur@skynet.be (Bart Lateur)
Subject: Re: REGEX tutorials ?
Message-Id: <3904b496.180868@news.skynet.be>

Stumpy wrote:

>I'm aware there are many Perl tutorials available on the www.  And that
>most of these tutorials cover REGEX to some degree or another.
>
>Is there one or two the members of this newsgroup consider better
>coverage / explanation of REGEX than the others ?

What would you like? How to use regexes, or learn how they work?

Use: the tutorial that ment a lot to me personally, is Tom
Christaensen's introductionary text. Original title: "Irregular
Expressions", currently retitled "PERL5 Regular Expression Description"
(boring!), URL: <http://language.perl.com/all_about/regexps.html>

Inner workings: Mark Jason Dominus' intro is really nice. "How Regexes
Work", URL: <http://www.plover.com/~mjd/perl/Regex/>. But currentl, the
site seems do be down. This has already been mentioned about a week ago.

If all of that still isn't enough, and you still want more, take a look
at the book "Mastering Regular Expresssions" by Jeffrey Friedl
(O'Reilly). Apart from the fact that I don't agree with Friedl's use of
the terms "NFA" and "DFA".

Find these and some other links at the links section of <www.perl.com>:
<http://www.perl.com/reference/query.cgi?section=regexp&x=6&y=11>

-- 
	Bart.


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

Date: Mon, 24 Apr 2000 22:46:17 +0200
From: "root" <ver@sims.nl>
Subject: Scoping trouble with my $foo
Message-Id: <3904b331$0$4850@reader2>

Hi all.
When i declare a var in a sub using my() i can't refer to it from whitin a
sub in this sub.
I don't think using local() is the best solution for a $temp inhere.
How should i solve this?

Regards,
Henry Vermeulen

--
**FATAL ERROR! HIT ANY USER TO CONTINUE**




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

Date: 24 Apr 2000 15:51:22 -0500
From: Tony Curtis <tony_curtis32@yahoo.com>
Subject: Re: Scoping trouble with my $foo
Message-Id: <87n1mjte7p.fsf@shleppie.uh.edu>

>> On Mon, 24 Apr 2000 22:46:17 +0200,
>> "root" <ver@sims.nl> said:

> Hi all.  When i declare a var in a sub using my() i
> can't refer to it from whitin a sub in this sub.  I
> don't think using local() is the best solution for a
> $temp inhere.  How should i solve this?

Pass it as a parameter, as nature intended...

hth
t


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

Date: Mon, 24 Apr 2000 19:03:29 GMT
From: Michael Stassen <stassenm@my-deja.com>
Subject: Re: sendmail problem in the perl script
Message-Id: <8e25pq$470$1@nnrp1.deja.com>

In article <8dj949$9af$1@nnrp1.deja.com>,
  Raymond <raymondchanth@my-deja.com> wrote:
<snip>
> I have already tested both open() and close():
> open(MAIL,"|sendmail -t") or die "Can't open sendmail: $!";
> ...
> close(MAIL) or die "Can't close sendmail: $!";
>
> but still got Internal Server Error. Any idea about this ?
> Thank you very much.
>
> Raymond

Raymond,

Since you get "Internal Server Error", I'm guessing this is part of a
cgi script.  If a cgi script exits without outputting a valid http
header, you get an internal server error.  When sendmail returns an
error, your close(MAIL) returns 0, so your code dies with the message
"Can't close sendmail...".  I expect your web server's error log
contains a "Can't close sendmail..." line for each time you've gotten
the internal server error.  If you want to avoid an internal server
error, your script needs to produce valid output in the face of an error
instead of simply calling die.  At a minimum, you need something like

close(MAIL) or (print "Content-type: text/html\n\nSorry, sendmail
returned an error\n" and exit);

Michael


Sent via Deja.com http://www.deja.com/
Before you buy.


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

Date: 24 Apr 2000 09:21:14 +0100
From: Jonathan Stowe <gellyfish@gellyfish.com>
Subject: Re: Show me if you don't mind
Message-Id: <8e105q$ha$1@orpheus.gellyfish.com>

On Sun, 23 Apr 2000 23:54:02 -0400 RMB wrote:
> Hey d'ere-
> 
> For you seasoned Perl programmers, I had a question. Could you send me some
> of your code and a description of what it does? I would like to see what
> nicely written Perl script looks like.
> 
> I just begun learning Perl and I would like to see some real-world perl
> script.
> 

Well this isnt 'real world' but its quite amusing : it produces a random
Dragon Outline fractal for use perhaps as a background or image on a
web page :


#!/usr/bin/perl -w

use CGI qw(:standard);
use strict;
use GD;

use vars qw($magnitude $P $Q $scale);

my ($x_centre,$y_centre) = (320,175);

if ( scalar param() < 3 )
  {
    my @configs = map {chomp; [ split ]} <DATA>; 
    ($P,$Q,$scale) = @{ $configs[rand @configs] };
   }
else
  {
   $P = param('real');
   $Q = param('imaginary');
   $scale = param('scale');
  }

   my $image = new GD::Image($x_centre * 2,$y_centre * 2);

   my $back_colour = $image->colorAllocate(255,255,255);
   my $fore_colour = $image->colorAllocate(0,0,0);

   my ($x,$y) = (0.50001,0);

   $magnitude = ($P*$P) + ($Q*$Q);

   $P = 4*$P/$magnitude;
   $Q = -4*$Q/$magnitude;

   $scale = $x_centre * $scale;

   for ( 1 .. 12000 )
      {
        my $temp_x = ($x * $P ) - ($y * $Q );
        $y = ($x * $Q) + ($y * $P );
        my $temp_y = $y;
        $x = 1 - $temp_x;

        $magnitude = sqrt(($x * $x ) + ($y * $y));

        $y = sqrt((-$x + $magnitude)/2);
        $x = sqrt(($x + $magnitude)/2);

        ($x = -$x) if ($temp_y < 0 );

        if ( rand() < 0.5 )
          {
            $x = -$x;
            $y = -$y;
          }

        $x = (1- $x) / 2;

        $y = $y / 2;

        my $col = int(($scale * ($x - 0.5 )) + $x_centre);
        my $row = int($y_centre - ($scale * $y));

         if ( ($_ > 0 ) &&
              ($col >= 0 ) &&
              ($col < (2 * $x_centre)) &&
              ($row >= 0 ) &&
              ($row < ($y_centre * 2 )))
            {
              $image->setPixel($col,$row,$fore_colour);
            }
     }

   print header('image/png');
   binmode(STDOUT);
   print $image->png;
__END__
1.646009    0.967049 0.75
2.447261    -0.967049 0.75
1.325508    0.786533 0.75
1.415414    0.856803 0.75
1.255399    0.691977 0.75
2.792809    -0.657593 0.75
3.018153    -0.098854 0.75
2.998122    0.004298 0.75


/J\
-- 
Mr. Scorpio says productivity is up 2, and it's all because of my
motivational techniques, like donuts and the possibility of more donuts
to come.
-- 
fortune oscar homer


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

Date: Mon, 24 Apr 2000 14:47:07 -0700
From: Tom Phoenix <rootbeer@redcat.com>
Subject: Re: socket fd exit with child?
Message-Id: <Pine.GSO.4.10.10004241444580.25963-100000@user2.teleport.com>

On Mon, 24 Apr 2000, steve wrote:

> if you exit the child process via an alarm, do you have to explicitly
> close the socket or will the OS close the socket fd and any other fd
> belonging to the child automatically upon child exit?

AFAIK, every species of OS will close any socket when there's no longer a
live process attached. Then again, there are a lot of species of OS on
which I haven't checked this. :-)

-- 
Tom Phoenix       Perl Training and Hacking       Esperanto
Randal Schwartz Case:     http://www.rahul.net/jeffrey/ovs/



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

Date: Mon, 24 Apr 2000 19:05:18 GMT
From: Hardy Merrill <hmerrill@my-deja.com>
Subject: store hex in db, and retrieve as hex
Message-Id: <8e25t6$491$1@nnrp1.deja.com>

I'm working with hex numbers in bitwise operations in CGI scripts.  I'd
like to store a hex number in an Oracle table, and be able to retrieve
that hex number and use it *AS A HEX NUMBER(?)* in bitwise comparisons,
so that I don't have to convert from hex to decimal and back again.  The
only method I've found so far is to take the hex number in the CGI
script, store it in Oracle as a VARCHAR2(character representation), and
read it out into a decimal number with an Oracle TO_NUMBER - conversions
all over the place.

I know this can be done better/easier - ideas?

TIA.
--
Hardy Merrill
Mission Critical Linux
http://www.missioncriticallinux.com


Sent via Deja.com http://www.deja.com/
Before you buy.


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

Date: 24 Apr 2000 19:35:13 GMT
From: gbacon@ruby.itsc.uah.edu (Greg Bacon)
Subject: Re: store hex in db, and retrieve as hex
Message-Id: <8e27lh$e6v$1@info2.uah.edu>

In article <8e25t6$491$1@nnrp1.deja.com>,
	Hardy Merrill <hmerrill@my-deja.com> writes:

: I'm working with hex numbers in bitwise operations in CGI scripts.  I'd
: like to store a hex number in an Oracle table, and be able to retrieve
: that hex number and use it *AS A HEX NUMBER(?)* in bitwise comparisons,
: so that I don't have to convert from hex to decimal and back again.  The
: only method I've found so far is to take the hex number in the CGI
: script, store it in Oracle as a VARCHAR2(character representation), and
: read it out into a decimal number with an Oracle TO_NUMBER - conversions
: all over the place.

Why do you care about the internal representation?  Store it as a number
in your database, and when you want to output it as a hexadecimal
number, then use sprintf "%x" => $num.  Otherwise, don't worry about it.

Greg
-- 
I want to die peacefully in my sleep like my grandfather, not screaming in
terror like his passengers.


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

Date: 24 Apr 2000 09:24:31 +0100
From: Jonathan Stowe <gellyfish@gellyfish.com>
Subject: Re: to print a flat data file with a certain lengths ?
Message-Id: <8e10bv$hf$1@orpheus.gellyfish.com>

On Mon, 24 Apr 2000 16:32:05 +0900 Joe wrote:
> Hi,
> I would like to print a flat data file with a certain lengths.
> 
> We guess we try to print the following line.
> print FILE "$a1|$a2|$a3\n";
> The maxlength of each value should be 100 or something else.
> 
> If the real text length of $a1 is smaller than 100, how to I leave the blank
> text space until to be before print |$a2 ?
> I mean it should be printed like the following;
> aaaaaaaa blank spaces|$a2|$a3
> or,
> blank spaces aaaaaaaa|$a2|$a3
> 
> If the length of $a1 is 40, the blank spaces should 60, than starting to
> print nest value.
> 
> If someone know about this, Please direct me.

You probably want to use sprintf() or possibly pack() both of which you
can read about in the perlfunc manpage.  If you are using fixed width fields
you might consider eschewing the '|' separators and use unpack() to
decompose the records when reading the file (of course this might be
for an external program which requires this format).

/J\
-- 
Read your town charter, boy. If food stuff should touch the ground,
said food stuff shall be turned over to the village idiot.? Since I
don't see him around, start shovelling!
-- 
fortune oscar homer


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

Date: Mon, 24 Apr 2000 12:13:18 -0700
From: Makarand Kulkarni <makarand_kulkarni@My-Deja.com>
Subject: Re: to print a flat data file with a certain lengths ?
Message-Id: <39049CCE.E370C3DA@My-Deja.com>

>
> If the real text length of $a1 is smaller than 100, how to I leave the blank
> text space until to be before print |$a2 ?

declare a picture format for use with write () and use it
perldoc  -f  format
perldoc -f write




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

Date: 24 Apr 2000 09:15:32 +0100
From: Jonathan Stowe <gellyfish@gellyfish.com>
Subject: Re: URGENT help needed
Message-Id: <8e0vr4$go$1@orpheus.gellyfish.com>

On Mon, 24 Apr 2000 07:15:45 GMT Lobo wrote:
> Hi there.
> 
> I'm trying to open a .shtml file from a perl script. The filename come
> from a form, like this:
> 
> <input type="hidden" name="file" value="foo">
> 
> so the script does this:
> 
> $temp=$ENV{'QUERY_STRING'};
> @pairs=split(/&/,$temp);
> foreach $item(@pairs) {
> ($key,$content)=split (/=/,$item,2);
> $content=~tr/+/ /;
> $content=~ s/%(..)/pack("c",hex($1))/ge;
> $fields{$key}=$content;
> }
> 
> $page = $fields{'file'}.".shtml";
> $txt = $fields{'file'}.".txt";
> 
> $pagefull = "/usr/home/httpd/html/leiloes/show/".$page;
> 
> $txtfull = "/usr/home/httpd/html/leiloes/show/".$txt;
> 
> 
> open (HTM, '+>$pagefull');
> 
> print HTM "<html>
> <head>
> <title></title>
> </head>
> <body>
> <!--#include file=\"$txt\" -->
> </body>
> </html>";
> close (HTM);
> 
> I'm getting an Internal Server Error, and I think it's beacuse I
> didn't sepecified the Content of the output, like 'print
> "Content-type:text/html\n\n";'
> 
> The thing is that since I'm trying to open a SHTML file, if I put the
> text/html it doesn't work - it doesn't show the contents of 'foo.txt'.
> Is there a Content-type for SHTML? I've tryed "text/shtml" but it
> doesn't work...
> 
> BTW, this txt file is CSV (Comma Sparated Values) format, so how can I
> make the script to dosn't show the commas?
> 
> Oh yes, this cgi is called by the GET method, as you must noticed.
> 
> Please HELP ME!
> 

You *must* supply some kind of header.  Generally you cannot do what
you are trying to do anyway as the server will not the parse the content
produced by a CGI program for SSI stuff. 

You will want to ask in the group comp.infosystems.www.authoring.cgi if
you dont believe me.

Ah I see what you want to do now.  You need to issue a redirect to the
file that you have generated after you finished printing it.

#!/usr/bin/perl -w

use strict;

use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);

my $file = param('file');

my $page = "$file.shtml";
my $txt  = "$file.txt";
 
my $pagefull = "/usr/home/httpd/html/leiloes/show/$page";
 
my $txtfull = "/usr/home/httpd/html/leiloes/show/$txt";
 
 
# The single quotes you had would have cause the variable
# not to be interpolated - also the '+' was redundant.

open (HTM, ">$pagefull") || die "Cant open $pagefull - $!\n";
 
 print HTM <<EEBAHGUM;
<html>
 <head>
 <title></title>
 </head>
 <body>
 <!--#include file=\"$txt\" -->
 </body>
 </html>
EEBAHGUM

close (HTM);
 
redirect("/show/$page");

You will want to change the above to reflect the actual URL of the
generated page.

/J\
-- 
The only thing that the artist cannot see is the obvious. The only thing
that the public can see is the obvious. The result is the Criticism of
the Journalist.
-- 
fortune oscar homer


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

Date: Mon, 24 Apr 2000 11:05:23 -0800
From: Joseph Loo <loo02@attglobal.net>
Subject: Using File::stat
Message-Id: <69000095659952330148510@MYHOSTNAME>

I am relative new in the use of Perl. I am trying to use the File::stat 
module under 5.00553. basically the code is:

use File::stat;

 .
 .
 .

$stats = stat("check_backup.cmd")
print $stat->mtime;

I get the following error message:

Option catalog_dir[0] - G:\Utils\Disk\ba2ksv\catalogs
Can't call method "mtime" without a package or object reference at Check_Backup.
cmd line 61.

I also tried importing the st_mtime names but I don't seem to understand
the requirements.

I tried use File::stat qw(:FIELDS)
When I try to print $st_mtime it comes back and requires a fully 
qualified name. I have use strict and -w turned on.

Joseph Loo
jloo@acm.org




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

Date: 24 Apr 2000 13:33:22 -0500
From: Tony Curtis <tony_curtis32@yahoo.com>
Subject: Re: Using File::stat
Message-Id: <877ldncpsd.fsf@shleppie.uh.edu>

>> On Mon, 24 Apr 2000 11:05:23 -0800,
>> Joseph Loo <loo02@attglobal.net> said:

> $stats = stat("check_backup.cmd")

Syntax error: missing ';' at end

Please post the real code, not something that looks
vaguely like the code you're using.

> print $stat->mtime;
        ^^^^^
What's the name of the variable above?

hth
t


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

Date: Mon, 24 Apr 2000 11:38:49 -0800
From: Joseph Loo <loo02@attglobal.net>
Subject: Re: Using File::stat
Message-Id: <27992295660152921633246@MYHOSTNAME>

I just figure it out why it does not work. It seems that the method 
belongs in a specific package File::stat. If you use a package 
definition after the use File::stat, it will not work properly.

evidently, the instance has not been blessed. I am still trying to 
figure out how to call the method in another package.

On Mon, 24 Apr 2000 11:05:23 -0800, Joseph Loo wrote:

>I am relative new in the use of Perl. I am trying to use the File::stat 
>module under 5.00553. basically the code is:
>
>use File::stat;
>
>.
>.
>.
>
>$stats = stat("check_backup.cmd")
>print $stat->mtime;
>
>I get the following error message:
>
>Option catalog_dir[0] - G:\Utils\Disk\ba2ksv\catalogs
>Can't call method "mtime" without a package or object reference at Check_Backup.
>cmd line 61.
>
>I also tried importing the st_mtime names but I don't seem to understand
>the requirements.
>
>I tried use File::stat qw(:FIELDS)
>When I try to print $st_mtime it comes back and requires a fully 
>qualified name. I have use strict and -w turned on.
>
>Joseph Loo
>jloo@acm.org
>
>

Joseph Loo
jloo@acm.org




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

Date: Mon, 24 Apr 2000 18:59:58 GMT
From: David Ness <DNess@Home.Com>
Subject: Re: Using File::stat
Message-Id: <390499B6.B8C83F66@Home.Com>


Joseph Loo wrote:
> 
> I just figure it out why it does not work. It seems that the method
> belongs in a specific package File::stat. If you use a package
> definition after the use File::stat, it will not work properly.
> 
> evidently, the instance has not been blessed. I am still trying to
> figure out how to call the method in another package.
> 
> On Mon, 24 Apr 2000 11:05:23 -0800, Joseph Loo wrote:
> 
> >I am relative new in the use of Perl. I am trying to use the File::stat
> >module under 5.00553. basically the code is:
> >
> >use File::stat;
> >
[snip]

What are you trying to do, and won't plain old `stat(...)' do it?


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

Date: Mon, 24 Apr 2000 21:28:42 GMT
From: David Ness <DNess@Home.Com>
Subject: Re: Which book?
Message-Id: <3904BC91.A64FB041@Home.Com>

mark_g@cyberdude.com wrote:
> 
> Which of the O'Reilly Perl books is best for someone who knows a little
> bit of Perl programming but wants a more complete knowledge of the
> language, "Learning Perl" or "Programming Perl" ?
> 
> Mark Grocock (mark_g@cyberdude.com)
> 

IMO most people have enough trouble knowing what books are good for themself, 
much less knowing what would be good for someone else, particularly someone they
don't know. That said, I think you can count on both of the books you
mention to be excellent (I find that true of most O'Reilly pubs) and if I
were you I'd go to a good old `physical bookstore' (B&N, Borders, ...) and
give them both a look, and then order from Amazon or some place like that who
will be willing to lose money shipping them to you...


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

Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 16 Sep 99)
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: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.

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 V9 Issue 2855
**************************************


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