[31459] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2711 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Dec 9 14:09:38 2009

Date: Wed, 9 Dec 2009 11:09:05 -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           Wed, 9 Dec 2009     Volume: 11 Number: 2711

Today's topics:
        Any way to tell if a scalar is a string? <jl_post@hotmail.com>
    Re: File::Temp help <sreservoir@gmail.com>
    Re: Finding expiration date of cookie <smallpond@juno.com>
    Re: Perl to Java bytecode <ben@morrow.me.uk>
    Re: Unblessed reference. <douglasg.wilson@gmail.com>
    Re: Unblessed reference. <ben@morrow.me.uk>
    Re: Unblessed reference. (Jens Thoms Toerring)
    Re: Unblessed reference. sln@netherlands.com
    Re: Unblessed reference. <justin.0911@purestblue.com>
    Re: Unblessed reference. <tadmc@seesig.invalid>
    Re: Unblessed reference. <justin.0911@purestblue.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 9 Dec 2009 09:35:51 -0800 (PST)
From: "jl_post@hotmail.com" <jl_post@hotmail.com>
Subject: Any way to tell if a scalar is a string?
Message-Id: <3bd945e4-452a-4540-9dac-3d1d65628550@h40g2000prf.googlegroups.com>

Hi,

   I'm wondering if there's a way in Perl to tell if a scalar was set
as a string.  For example, I'd like to write code like this:

   my $var1 = "7777";
   my $var2 = 7777;

   foreach ($var1, $var2)
   {
      if ( SOME_TEST )
      {
         # Value is a string, so print it with quotes:
         print "Value = \"$_\"\n";
      }
      else
      {
         # Print it without quotes:
         print "Value = $_\n";
      }
   }

   The reason I want to do this is because I write a lot of Perl code
that uses the unpack() function, and then reports the data it
extracts.  For example, I might write:

    $string = 'A 65';
    @data = unpack('c x a2', $string);

in which case @data should be filled with two elements:  the first
being the integer 65, and the second the string "65".  But since Perl
seamlessly converts strings to the numeric values they contain, it can
be hard for me to know whether a value that looks numeric was
originally a string or a number.

   I noticed that if I use the perl debugger (with "perl -wde 1"),
typing "x @data" does not show which element is a string.  On the
other hand, "use Data::Dumper; print Dumper @data;" DOES show the
difference -- and it shows it by surrounding the second element with
single quotes.  (Evidently Perl does keep track of whether a numeric
scalar was set as a string or not.)

   So my question is:  How can I know if a scalar (that looks like a
number) was originally a string?  (That is, assigned/encoded to a
string?)

   I've read "perldoc Scalar::Util", but the only functions that look
related are looks_like_number() (which is not what I want because
regardless of whether it looks like a number I want to know if it was
encoded as a string) and isvstring() (which returns true if the value
was coded as a "vstring" (but not as a string, which is what I want)).

   Thanks in advance for any help.

   -- Jean-Luc


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

Date: Tue, 08 Dec 2009 16:04:46 -0500
From: sreservoir <sreservoir@gmail.com>
Subject: Re: File::Temp help
Message-Id: <hfmf0e$pgg$1@aioe.org>

Ben Morrow wrote:
> Quoth hymie@lactose.homelinux.net (hymie!):
>> Greetings.
>>
>> I must be missing something.  I hope somebody can tell me what it is.
>>
>> Short question:
>>
>> I'm trying to use a temporary file.  However,
>>
>> my $fh = tempfile();
>> while (<FILE>)
>> {
>>   print $fh $_;
>> }
>>
>> only results in an empty file.
> 
> Files created with tempfile will normally be deleted when the process
> exits, or under some systems as soon as the file is closed. How are you
> discovering the file is empty? You don't ask File::Temp for the
> filename...
> 
>> Long question:
>>
>> According to the perldoc for File::Temp
>>
>> tmpfile
>>     Returns the filehandle of a temporary file.
>>       $fh = tmpfile();
>>
>> Now, I was under the impression that "the filehandle of a temporarly file"
>> is an incomplete concept -- that part of the definition of a filehandle
>> includes whether the file is open for reading or writing.  So I'm not I
>> understand exactly what is being returned from the tempfile() command,
>> or how I can use it to write data into a temporary file.
> 
> tempfile returns a handle open for both reading and writing. In Perl
> terms, the file is opened with the "<+" mode.

actually more like >+. There really is no perl analogue; it's O_CREAT|
O_EXCL|O_RDWR, like, 'create if nonexistent, open only if nonexistent, 
open for both reading and writing,' atomically.

-- 

   "Six by nine. Forty two."
   "That's it. That's all there is."
   "I always thought something was fundamentally wrong with the universe"


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

Date: Tue, 8 Dec 2009 12:49:50 -0800 (PST)
From: smallpond <smallpond@juno.com>
Subject: Re: Finding expiration date of cookie
Message-Id: <c015794a-6e4e-4131-8c43-e2c3909a908a@n35g2000yqm.googlegroups.com>

On Dec 8, 4:07=A0am, Jason Carlton <jwcarl...@gmail.com> wrote:
> On Dec 7, 11:19=A0pm, smallpond <smallp...@juno.com> wrote:
>
> > On Dec 7, 8:27=A0pm, Jason Carlton <jwcarl...@gmail.com> wrote:
>
> > > Is there any way to find the expiration date of a set cookie?
>
> > > I'm setting a cookie in one script, and the expiration date is set
> > > dynamically. In another script, I want to set another cookie, and wan=
t
> > > to match the expiration date to the one set in the earlier script.
>
> > perldoc CGI
>
> I've looked there, and didn't see any way to retrieve this
> information. I know how to set it (obviously), just not retrieve it.
>
> Can you be more specific?


Is it not in the hash when you read the cookie?
What do you get when you do this:
               %answers =3D cookie('answers');
               $k =3D join "\n", keys %answers
               print $k;


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

Date: Tue, 8 Dec 2009 19:42:49 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Perl to Java bytecode
Message-Id: <pqm2v6-cjl.ln1@osiris.mauzo.dyndns.org>


Quoth ccc31807 <cartercc@gmail.com>:
> Searching the archives, I found old posts, but nothing recent. Is
> there anything going on with Perl to match Scala or Clojure or Jython,
> i.e., a compilation targeted to the JVM?

Not AFAIK. It would be borderline-impossible, given the huge prevelance
of XS extensions that play fast and loose with perl's internals. The
Ponie project (Perl 5 on Parrot, which is likely to be a better fit than
the JVM to start with) was eventually abandoned for exactly that reason.

Ben



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

Date: Tue, 8 Dec 2009 11:32:59 -0800 (PST)
From: "DouglasG.Wilson@gmail.com" <douglasg.wilson@gmail.com>
Subject: Re: Unblessed reference.
Message-Id: <d1ab89c4-5a8a-421e-b460-3554e9ea682d@s31g2000yqs.googlegroups.com>

On Dec 8, 10:45=A0am, Justin C <justin.0...@purestblue.com> wrote:
> I'm using Spreadsheet::WriteExcel, and (I think) I've created a
> reference to the worksheet. When I try to use this elsewhere I get:
> Can't call method ... on unblessed reference at ...
>
> This is the minumum I've been able to cut the code down to (sorry it's
> not shorter). The problem line is 26. As per the Spreadsheet::WriteExcel
> documentation I've created the object, and it's a reference to that
> object that I've passed from a subroutine to Main:: to pass on to other
> subroutines.
>
> I'm slowly getting to grips with references, I seem to be OK (mostly)
> with scalar, array, and harh refs, but what is this? An object
> reference? I'm working on it but I haven't got it yet. Anyway, here's
> the code.
>
>
> my ($worksheet, $format) =3D create_excel_file();

You're returning references to the worksheet and format objects here:
> =A0 =A0 return (\$ws, \$format);}
Just return the objects:
     return ($ws, $format);}

Then this should work:
> =A0 =A0 $worksheet->write_row($row,$start_col,$items,$format);}

HTH,
- Douglas Wilson


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

Date: Tue, 8 Dec 2009 19:40:43 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Unblessed reference.
Message-Id: <rmm2v6-cjl.ln1@osiris.mauzo.dyndns.org>


Quoth Justin C <justin.0911@purestblue.com>:
> 
> I'm using Spreadsheet::WriteExcel, and (I think) I've created a
> reference to the worksheet. When I try to use this elsewhere I get:
> Can't call method ... on unblessed reference at ...
> 
> This is the minumum I've been able to cut the code down to (sorry it's
> not shorter). The problem line is 26. As per the Spreadsheet::WriteExcel
> documentation I've created the object, and it's a reference to that
> object that I've passed from a subroutine to Main:: to pass on to other
> subroutines.

An object is already a reference. You don't need to take another layer
of reference, just return the object.

> I'm slowly getting to grips with references, I seem to be OK (mostly)
> with scalar, array, and harh refs, but what is this? An object
> reference? I'm working on it but I haven't got it yet. Anyway, here's
> the code. 
> 
> #!/usr/bin/perl
> 
> use warnings;
> use strict;
> use Spreadsheet::WriteExcel;
> 
> my ($worksheet, $format) = create_excel_file();
> 
> artist_chart() ;

Don't pass parameters to subs through globals. It's very confusing.

    artist_chart($worksheet, $format);

> sub artist_chart {

(Add the parameters here, and pass them to populate_spreadsheet, of
course.)

>     my ($position, $last_month, $artist) = ('7', '4', 'Banksy');
>     populate_spreadsheet(0, $position, $last_month, $artist);
> }
> sub create_excel_file {
>     my $fname = "/var/local/chart/boris.xls";
>     my $workbook = Spreadsheet::WriteExcel->new($fname);
>     my $ws = $workbook->add_worksheet();
>     my $format = set_sheet_table_borders($workbook);
>     return (\$ws, \$format);

Just leave off the '\'s here.

    return ($ws, $format);

Ben



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

Date: 8 Dec 2009 19:49:06 GMT
From: jt@toerring.de (Jens Thoms Toerring)
Subject: Re: Unblessed reference.
Message-Id: <7o7otiF3oq34fU1@mid.uni-berlin.de>

Justin C <justin.0911@purestblue.com> wrote:

> I'm using Spreadsheet::WriteExcel, and (I think) I've created a
> reference to the worksheet. When I try to use this elsewhere I get:
> Can't call method ... on unblessed reference at ...

> This is the minumum I've been able to cut the code down to (sorry it's
> not shorter). The problem line is 26. As per the Spreadsheet::WriteExcel
> documentation I've created the object, 

You asked for an object to be created and what you get back is
already a reference - objects are references, just, due to being
bless'ed, with some extra magic attached (and you shouldn't need
to care what that reference points to, that's what objects are
all about). Thus you add one unnecessary level of indirection
when you return a reference to what you got from the functions
that created the objects.

> and it's a reference to that
> object that I've passed from a subroutine to Main:: to pass on to other
> subroutines.

And that's not the right thing to do, in the routine the reference
to the object gets passed to you need the object (a blessed refe-
rence) itself.

> I'm slowly getting to grips with references, I seem to be OK (mostly)
> with scalar, array, and harh refs, but what is this? An object
> reference? I'm working on it but I haven't got it yet. Anyway, here's
> the code. 

> #!/usr/bin/perl

> use warnings;
> use strict;
> use Spreadsheet::WriteExcel;

> my ($worksheet, $format) = create_excel_file();

> artist_chart() ;

> sub artist_chart {
>     my ($position, $last_month, $artist) = ('7', '4', 'Banksy');
>     populate_spreadsheet(0, $position, $last_month, $artist);
> }
> sub create_excel_file {
>     my $fname = "/var/local/chart/boris.xls";
>     my $workbook = Spreadsheet::WriteExcel->new($fname);

First object created.

>     my $ws = $workbook->add_worksheet();

And a "worksheet" object.

>     my $format = set_sheet_table_borders($workbook);

And here you create a "format" object.

>     return (\$ws, \$format);

And here you pass back references to two to of the objects which
already are references. That's verkill and it doesn't look as if
it is what you really want to do. Leave it at

       return ( $ws, $format );
> }

and you rather likely will be fine, especially since the
way you use them looks very much as if you need the object
itself and not a reference to it. I.e. when you later do

> sub populate_spreadsheet {
>     my $start_col = shift;
>     my $items = \@_;
>     my $row = $_[0] + 1;
>     $worksheet->write_row($row,$start_col,$items,$format);
> }

you need a "worksheet" object and not a reference to such
an object - or you would need to dereference $worksheet at
this place to get back the object itself.

> sub set_sheet_table_borders {
>     my $wb = shift;
>     my $format = $wb->add_format(
>         border => 7,
>     );
>     return $format;
> }
                            Regards, Jens
-- 
  \   Jens Thoms Toerring  ___      jt@toerring.de
   \__________________________      http://toerring.de


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

Date: Tue, 08 Dec 2009 14:10:37 -0800
From: sln@netherlands.com
Subject: Re: Unblessed reference.
Message-Id: <qigth5l10p7l3luqm6dnetf499ku417t53@4ax.com>

On Tue, 08 Dec 2009 18:45:19 -0000, Justin C <justin.0911@purestblue.com> wrote:

>
>I'm using Spreadsheet::WriteExcel, and (I think) I've created a
>reference to the worksheet. When I try to use this elsewhere I get:
>Can't call method ... on unblessed reference at ...
>
>This is the minumum I've been able to cut the code down to (sorry it's
>not shorter). The problem line is 26. As per the Spreadsheet::WriteExcel
>documentation I've created the object, and it's a reference to that
>object that I've passed from a subroutine to Main:: to pass on to other
>subroutines.
>
>I'm slowly getting to grips with references, I seem to be OK (mostly)
>with scalar, array, and harh refs, but what is this? An object
>reference? I'm working on it but I haven't got it yet. Anyway, here's
>the code. 
>
>#!/usr/bin/perl
>
>use warnings;
>use strict;
>use Spreadsheet::WriteExcel;
>
>my ($worksheet, $format) = create_excel_file();
     ^^^^^^^^^^^^^^^^^^^
Never worked with this module, just guessing.
You return reference's (\$ws, \$format).
$workbook->add_worksheet(); and
$wb->add_format(), return references, then you
are returning references to references.
When you dereference by adding the '->' arrow,
$worksheet->'something', it is looking for a method
of a class, an array, hash, ... not another reference.

What happened to $workbook? Don't need it anymore?
Its never stored. Unless of course $workbook->add_worksheet();
creates worksheet object as a wrapper and stores itself ($self)
in a variable of the worksheet object. But that seems unlikely.

>
>artist_chart() ;
>
>sub artist_chart {
>    my ($position, $last_month, $artist) = ('7', '4', 'Banksy');
>    populate_spreadsheet(0, $position, $last_month, $artist);
>}
>sub create_excel_file {
>    my $fname = "/var/local/chart/boris.xls";
>    my $workbook = Spreadsheet::WriteExcel->new($fname);
>    my $ws = $workbook->add_worksheet();
>    my $format = set_sheet_table_borders($workbook);
                                          ^^^^^^^^^
Goes away, lost forever!

>    return (\$ws, \$format);
>}
>sub populate_spreadsheet {
>    my $start_col = shift;
>    my $items = \@_;
>    my $row = $_[0] + 1;
>    $worksheet->write_row($row,$start_col,$items,$format);
                                           ^^^^^^
@_ appears to be singularly scoped to each function, so this
cannot be itterated. Its no big deal, but generally, create
an independent (new) array reference everytime this gets
called  .. my $items = [@_];

Also, I'm sure you are aware that when items is assigned, @_
contains  ($position, $last_month, $artist).

>}
>sub set_sheet_table_borders {
>    my $wb = shift;
        ^^
Here you pass in workbook object, then later it is discarded.
In the function populate_spreadsheet(),
you use the global variables ($worksheet, $format).
This does not have general portability and its hard to tell
where/what these variables are.
I would try to consistently generalize helper functions without
the use/need of globals.

If you have groups of global variables specific to each other,
as some result of making multiple objects, maybe you could store
them in an array of hashs'.

-sln


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

Date: Wed, 09 Dec 2009 16:51:00 -0000
From: Justin C <justin.0911@purestblue.com>
Subject: Re: Unblessed reference.
Message-Id: <5a26.4b1fd574.b0d3a@zem>

On 2009-12-08, Ben Morrow <ben@morrow.me.uk> wrote:
>
> Quoth Justin C <justin.0911@purestblue.com>:
>> 
>> I'm using Spreadsheet::WriteExcel, and (I think) I've created a
>> reference to the worksheet. When I try to use this elsewhere I get:
>> Can't call method ... on unblessed reference at ...
>> 
>> This is the minumum I've been able to cut the code down to (sorry it's
>> not shorter). The problem line is 26. As per the Spreadsheet::WriteExcel
>> documentation I've created the object, and it's a reference to that
>> object that I've passed from a subroutine to Main:: to pass on to other
>> subroutines.
>
> An object is already a reference. You don't need to take another layer
> of reference, just return the object.

I think I'm getting the hang. I've been using references for ages,
hashrefs, arrayrefs, but whenever I use objects I get a bit confused.
It's coming together slowly now that I'd started working through the OO
documentation.


>> my ($worksheet, $format) = create_excel_file();
>> 
>> artist_chart() ;
>
> Don't pass parameters to subs through globals. It's very confusing.
>
>     artist_chart($worksheet, $format);

OK, sounds sensible - I really should re-read perlstyle.

Thank you for the suggestions.

	Justin.

-- 
Justin C, by the sea.


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

Date: Wed, 09 Dec 2009 11:15:14 -0600
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Unblessed reference.
Message-Id: <slrnhhvmiu.69q.tadmc@tadbox.sbcglobal.net>

Justin C <justin.0911@purestblue.com> wrote:
> On 2009-12-08, Ben Morrow <ben@morrow.me.uk> wrote:
>> Quoth Justin C <justin.0911@purestblue.com>:


>>> my ($worksheet, $format) = create_excel_file();
>>> 
>>> artist_chart() ;
>>
>> Don't pass parameters to subs through globals. It's very confusing.
>>
>>     artist_chart($worksheet, $format);
>
> OK, sounds sensible - I really should re-read perlstyle.


Though rereading perlstyle is certainly a good idea, it does not
address the issue that Ben pointed out.

Probably because passing arguments rather than communicating with
subroutines via global variables is not an element of Perl style, 
it is an element of any-programming-language style.

That is, it is fundamental Computer Science.


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


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

Date: Wed, 09 Dec 2009 17:10:39 -0000
From: Justin C <justin.0911@purestblue.com>
Subject: Re: Unblessed reference.
Message-Id: <5ee3.4b1fda0f.13d46@zem>

On 2009-12-08, sln@netherlands.com <sln@netherlands.com> wrote:
> On Tue, 08 Dec 2009 18:45:19 -0000, Justin C <justin.0911@purestblue.com> wrote:
>>
>>my ($worksheet, $format) = create_excel_file();
>      ^^^^^^^^^^^^^^^^^^^
> Never worked with this module, just guessing.
> You return reference's (\$ws, \$format).
> $workbook->add_worksheet(); and
> $wb->add_format(), return references, then you
> are returning references to references.
> When you dereference by adding the '->' arrow,
> $worksheet->'something', it is looking for a method
> of a class, an array, hash, ... not another reference.
>
> What happened to $workbook? Don't need it anymore?
> Its never stored. Unless of course $workbook->add_worksheet();
> creates worksheet object as a wrapper and stores itself ($self)
> in a variable of the worksheet object. But that seems unlikely.

I admit that this is all a grey area to me, but I'm following examples
in the module, and that's how it says to do it. I think $worksheet
contains some reference back to the $workbook (so that it knows which
workbook it is, as there could be more than one).


>>artist_chart() ;
>>
>>sub artist_chart {
>>    my ($position, $last_month, $artist) = ('7', '4', 'Banksy');
>>    populate_spreadsheet(0, $position, $last_month, $artist);
>>}
>>sub create_excel_file {
>>    my $fname = "/var/local/chart/boris.xls";
>>    my $workbook = Spreadsheet::WriteExcel->new($fname);
>>    my $ws = $workbook->add_worksheet();
>>    my $format = set_sheet_table_borders($workbook);
>                                           ^^^^^^^^^
> Goes away, lost forever!

Other than to put a worksheet in it, and add formatting to it, it's not
needed - unless I want to explicitly close it.


>>    return (\$ws, \$format);
>>}
>>sub populate_spreadsheet {
>>    my $start_col = shift;
>>    my $items = \@_;
>>    my $row = $_[0] + 1;
>>    $worksheet->write_row($row,$start_col,$items,$format);
>                                            ^^^^^^
> @_ appears to be singularly scoped to each function, so this
> cannot be itterated. Its no big deal, but generally, create
> an independent (new) array reference everytime this gets
> called  .. my $items = [@_];
>
> Also, I'm sure you are aware that when items is assigned, @_
> contains  ($position, $last_month, $artist).

The ->write_row function (?) will populate a spreadsheet row, starting
at row $row, column $start_col and write each of @{$items} across the
columns until it runs out of @{$items}. For some reason it wants an
arrayref, not the array (according to the documentation).  

>
>>}
>>sub set_sheet_table_borders {
>>    my $wb = shift;
>         ^^
> Here you pass in workbook object, then later it is discarded.
> In the function populate_spreadsheet(),
> you use the global variables ($worksheet, $format).
> This does not have general portability and its hard to tell
> where/what these variables are.
> I would try to consistently generalize helper functions without
> the use/need of globals.
>
> If you have groups of global variables specific to each other,
> as some result of making multiple objects, maybe you could store
> them in an array of hashs'.

I readed perlref from time to time, each time I understand a little
more. I have a feeling that now I'm looking more at OO programming, and
using objects, I'm going to understand a bit more. I'll give it another
read, and also perlreftut. 

It's all going in slowly. I love it when I go back to my old code and
replace twenty lines with just a couple. Even though I've a long way to
go, I feel that I have still come quite far.

Thanks for the help. Also, thanks to others who replied but to whom I
have not followed up, repetition helps get the information to stick!

	Justin.

-- 
Justin C, by the sea.


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

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:

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

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#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 V11 Issue 2711
***************************************


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