[26073] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 8275 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Jul 25 03:05:21 2005

Date: Mon, 25 Jul 2005 00:05:06 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Mon, 25 Jul 2005     Volume: 10 Number: 8275

Today's topics:
    Re: "Can't return a temporary from lvalue subroutine... <tassilo.von.parseval@rwth-aachen.de>
        Emacs modules for Perl programming (Jari Aalto+mail.perl)
    Re: How does a scalar know what it is? <sisyphus1@nomail.afraid.org>
        Prepending to a file <nospam@nowhere.com>
    Re: Prepending to a file <noreply@gunnar.cc>
    Re: Question about hashes of hashes <lfs.1@spam.net>
        re-blessing a blessed reference <wiseguy@ihug.co.nz>
    Re: re-blessing a blessed reference <tassilo.von.parseval@rwth-aachen.de>
    Re: re-blessing a blessed reference <wiseguy@ihug.co.nz>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Mon, 25 Jul 2005 07:21:37 +0200
From: "Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de>
Subject: Re: "Can't return a temporary from lvalue subroutine..."
Message-Id: <slrnde8tn1.s1.tassilo.von.parseval@localhost.localdomain>

Also sprach Anno Siegel:

> Ilya Zakharevich  <nospam-abuse@ilyaz.org> wrote in comp.lang.perl.misc:
>> [A complimentary Cc of this posting was sent to
>> Anno Siegel
>> <anno4000@lublin.zrz.tu-berlin.de>], who wrote in article
>> <dbgjls$l1e$1@mamenchi.zrz.TU-Berlin.DE>:

>> > It looks like "temporary" refers to the state of a scalar of being
>> > "mortal".  Mortal SVs are all over the Perl source, but normally
>> > only concern XS programmers.  This is the first time I see the
>> > distinction appear at Perl level.
>> 
>> There is no way to determine that a thing is mortal (short of walking
>> up the mortalization stack and checking whether a value is there).
>
> Oh.  I thought of it as some kind of flag in the SV.  Tassilo had already
> hinted as much.  Obviously a little knowledge is a dangerous thing.
> Apologies for posting a mere guess.

It is a flag in the SV. And previously perl only checked a combination
of flags that would normally indicated that the variable in question is
mortal and thus makes no sense to be assigned to in a lvalue context.
In my patch I merely extended this check a little: It now checks the
mortality flags and only produces the error when the value is not also a
tied hash or array element (that is: the hash/array is tied, but for
elements of these perl internally uses PERL_MAGIC_tiedelem).

>> So, obviously, it is not that.  Here "a temporary" is an intermediate
>> variable created by Perl without a direct intervention of a programmer.
>> 
>> I'm afraid that it was me who put this error message there; so all the
>> blame on its wording may be on me.  When I introduced the lvalue subs,
>> there was a lot of suspicion on p5p that the whole idea is junk; as a
>> result, the handling is over-protecting.  This "can't" in the message,
>> for example, is red herring; there would be no problem for Perl to
>> change a value of a temporary; it is just that usually it is not what
>> a programmer wants:
>> 
>>   sub add5 : lvalue {
>>       my $x = shift;
>>       return $x + 5;
>>   }
>
> Ah, but that it apparently caught in a different place:
>
>     Can't modify addition (+) in lvalue subroutine return at ...
>
>> So the main usage of this message is to warn the programmer of
>> *possibly* flaky code.
>> 
>> The problem is that the current implementation of tied hashes is using
>> a temporary to handle access to a value for a key.  I did not realize
>> this when implementing the lvalue subs, so there is no special case to
>> handle this "legitimate" use of temporaries in lvalue subs.

There is now. I also wish perl didn't create temporaries for the access
to elements of tied variables (I complained about that in the journal
entry I posted a link to elsewhere in this thread). It requires a new
kind of magic for these temporaries and subsequently creates more
special cases that need to be dealt with individually.

There are other things that cannot be done with lvalue subs that I would
consider legitimate. For instance they only seem to work in scalar
context while it would be nice if they could also be used to assign to
arrays and hashes. That however could be tricky to fix.

Tassilo
-- 
use bigint;
$n=71423350343770280161397026330337371139054411854220053437565440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);


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

Date: 25 Jul 2005 04:29:36 GMT
From: <jari.aalto <AT> poboxes.com> (Jari Aalto+mail.perl)
Subject: Emacs modules for Perl programming
Message-Id: <perl-faq/emacs-lisp-modules_1122265746@rtfm.mit.edu>

Archive-name: perl-faq/emacs-lisp-modules
Posting-Frequency: 2 times a month
URL: http://tiny-tools.sourceforge.net/
Maintainer: Jari Aalto A T poboxes com

Announcement: "What Emacs lisp modules can help with programming Perl"

    Preface

        Emacs is your friend if you have to do anything comcerning software
        development: It offers plug-in modules, written in Emacs lisp
        (elisp) language, that makes all your programmings wishes come
        true. Please introduce yourself to Emacs and your programming era
        will get a new light.

    Where to find Emacs/XEmacs

        o   Unix:
            http://www.gnu.org/software/emacs/emacs.html
            http://www.xemacs.org/

        o   Unix Windows port (for Unix die-hards):
            install http://www.cygwin.com/  which includes native Emacs 21.x.
            and XEmacs port

        o   Pure Native Windows port
            http://www.gnu.org/software/emacs/windows/ntemacs.html
            ftp://ftp.xemacs.org/pub/xemacs/windows/setup.exe

        o   More Emacs resources at
            http://tiny-tools.sourceforge.net/  => Emacs resource page

Emacs Perl Modules

    Cperl -- Perl programming mode

        http://www.cpan.org/modules/by-authors/id/ILYAZ/cperl-mode/
        http://math.berkeley.edu/~ilya/software/emacs/
        by Ilya Zakharevich

        CPerl is major mode for editing perl files. Forget the default
        `perl-mode' that comes with Emacs, this is much better. Comes
        standard in newest Emacs.

    TinyPerl -- Perl related utilities

        http://tiny-tools.sourceforge.net/

        If you ever wonder how to deal with Perl POD pages or how to find
        documentation from all perl manpages, this package is for you.
        Couple of keystrokes and all the documentaion is in your hands.

        o   Instant function help: See documentation of `shift', `pop'...
        o   Show Perl manual pages in *pod* buffer
        o   Grep through all Perl manpages (.pod)
        o   Follow POD references e.g. [perlre] to next pod with RETURN
        o   Coloured pod pages with `font-lock'
        o   Separate `tiperl-pod-view-mode' for jumping topics and pages
            forward and backward in *pod* buffer.

        o   Update `$VERSION' variable with YYYY.MMDD on save.
        o   Load source code into Emacs, like Devel::DProf.pm
        o   Prepare script (version numbering) and Upload it to PAUSE
        o   Generate autoload STUBS (Devel::SelfStubber) for you
            Perl Module (.pm)

    TinyIgrep -- Perl Code browsing and easy grepping

        [TinyIgrep is included in Tiny Tools Kit]

        To grep from all installed Perl modules, define database to
        TinyIgrep. There is example file emacs-rc-tinyigrep.el that shows
        how to set up dattabases for Perl5, Perl4 whatever you have
        installed

        TinyIgrep calls Igrep.el to to do the search, You can adjust
        recursive grep options, set search case sensitivity, add user grep
        options etc.

        You can find latest `igrep.el' module at
        <http://groups.google.com/groups?group=gnu.emacs.sources> The
        maintainer is Jefin Rodgers <kevinr <AT> ihs.com>.

    TinyCompile -- To Browse grep results in Emacs *compile* buffer

        TinyCompile is a minor mode for *compile* buffer from where
        you can collapse unwanted lines or shorten file URLs:

            /asd/asd/asd/asd/ads/as/da/sd/as/as/asd/file1:NNN: MATCHED TEXT
            /asd/asd/asd/asd/ads/as/da/sd/as/as/asd/file2:NNN: MATCHED TEXT

            -->

            cd /asd/asd/asd/asd/ads/as/da/sd/as/as/asd/
            file1:NNN: MATCHED TEXT
            file1:NNN: MATCHED TEXT

End



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

Date: Mon, 25 Jul 2005 13:59:22 +1000
From: "Sisyphus" <sisyphus1@nomail.afraid.org>
Subject: Re: How does a scalar know what it is?
Message-Id: <42e463b2$0$27469$afc38c87@news.optusnet.com.au>


"Tom" <lfs.1@spam.net> wrote

>
> It looks to me that these scalars are carrying around
> alot of baggage.  Does it need to carry this baggage
> on the function calls?
>

Think of it as the scalars carrying around a pointer to the baggage (rather
than carrying around the actual baggage) - which is not such a big load to
be carrying.

Cheers,
Rob




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

Date: Sun, 24 Jul 2005 23:58:16 +0100
From: Dave <nospam@nowhere.com>
Subject: Prepending to a file
Message-Id: <42e41d09@212.67.96.135>

I would like to write something at the begginging of a text file, 
without overwriting what is already there.

i.e if the file 'foo' contains

two
three
four

I would like to be able to prepend 'one'

so the file reads

one
two
three
four

How is the best way to do this?

On a UNIX sheel script I could do it by writing the new data (in this 
case the word 'one') into another file, then using cat

But I am not sure how to do any of this in perl. I'm using a UNIX 
system, and Perl 5.8.6



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

Date: Mon, 25 Jul 2005 01:20:13 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Prepending to a file
Message-Id: <3kim1fFuhbleU1@individual.net>

Dave wrote:
> I would like to write something at the begginging of a text file, 
> without overwriting what is already there.

<snip>

> On a UNIX sheel script I could do it by writing the new data (in this 
> case the word 'one') into another file, then using cat
> 
> But I am not sure how to do any of this in perl.

     http://perldoc.perl.org/perlintro.html

     http://learn.perl.org/

Happy reading!

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


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

Date: Sun, 24 Jul 2005 17:18:50 -0500
From: "Tom" <lfs.1@spam.net>
Subject: Re: Question about hashes of hashes
Message-Id: <yM-dnV-oD5c6jnnfRVn-uA@comcast.com>


"Sven-Thorsten Fahrbach" <sven-thorsten.fahrbach@gmx.net> wrote in message
news:20050724231742.6eeb0ab2.sven-thorsten.fahrbach@gmx.net...
> On Sun, 24 Jul 2005 22:39:31 +0200
> djacober <djacober@swissonline.ch> wrote:
>
> > Hi all
> >
> > I tried to pass a hash of hashes as a reference to a subroutine but
> > everytime I try to make an operation with to the hash I get the
> > following mistake. I found a lot of information on the web but it's
> > still not clear to me what I can do about it
> >
> > Here's the error message I get:
> >
> > djacober@tux ~/perl $ perl -w detect_eyes.pl > eyes.out
> > Can't use string ("1") as a HASH ref while "strict refs" in use at
> > detect_eyes.pl line 126.
> >
> > Here's a the snippet of my code that causes the headache:
> >
> > sub detect_eye {
> >
> >    my $ref_color = @_;
>
> Wrong context. This doesn't mean 'give me the first element of @_' (which
is what you probably had in mind) but 'give me the number of elements in the
list'. You pass the hashref \%white to the sub, so @_ contains one element,
i.e. 1 is being assigned to $ref_color, hence the error message 'Can't use
string ("1")...'. What you want to do is either:
> 1 - The C way   : my $ref_color = $_[0]; or
> 2 - The Perl way: my $ref_color = shift;
> shift() operates on @_ if no additional arguments are given and you should
use it instead of accessing individual array elements by their subscripts,
which is slower.
> perldsc is also a good reference if you run into trouble with hashes of
hashes or other data structures.

I am still having trouble with why "1" is not X0001
or some such.




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

Date: Mon, 25 Jul 2005 14:12:35 +1200
From: Jon <wiseguy@ihug.co.nz>
Subject: re-blessing a blessed reference
Message-Id: <dc1ho3$59r$1@lust.ihug.co.nz>

Hiya,

I'm trying to tidy up my code a bit by creating an 'SQL' package. At the
moment to interface with my db I'm doing something like..

sub checkpass
{
        my ($username, $password) = @_;

        my $dbh = DBI->connect("DBI:mysql:database", "username", "password")
        unless ($dbh) { die($DBI::errstr); }

        my $sql = "SELECT id FROM users WHERE username=? AND password=?";
        my $sth = $dbh->prepare($sql);
        $sth->execute( $username, $password) || die($sth->errstr());
        
        my $rows = $sth->rows;

        $sth->finish();
        $dbh->disconnect();

        return $rows;
}

I'd like to be able to create a package so I only need to connect the
database once and I can disconnect when the script has finished. eg:

my $dbh = SQL->new();

if ($dbh->checkpass()) { #authed } else { #notauthed }

I've tried the code below but get it dies with "Can't locate object method
"prepare" via package "SQL" at /SQL.pm line 19. I'm pretty new to OO so I
can't figure out what I'm doing wrong. I understand that its trying to call
on SQL::prepare but I don't know how I can make it not do that. I have a
feeling its got something to do with me re-blessing $dbh which has already
been blessed but would like someone with more experience to confirm that if
possible. If I'm wrong could someone please point me in the right
direction? Thanks for your time.

=test.pl=
#!/usr/bin/perl

use lib './';
use SQL;
use strict;

my $dbh = SQL->new();
if ($dbh->checkpass( "username", "password" ))
{
        print "authed";
}
else
{
        print "not authed";
}

=SQL.pm=
Package SQL;

use DBI;
use strict;

sub new
{
        my $dbh = DBI->connect("DBI:mysql:database", "username", "password")
        unless ($dbh) { die($DBI::errstr); }

        bless $dbh, 'SQL';
        return $dbh;
}

sub checkpass
{
        my ($dbh, $username, $password);

        my $sql = "SELECT id FROM users WHERE username=? AND PASSWORD=?";
        my $sth = $dbh->prepare($sql);
        $sth->execute( $username, $password ) || die($sth->errstr));
        my $rows = $sth->rows;

        return $rows;
}

1;
-- 
Jon
wiseguy@ihug.co.nz


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

Date: Mon, 25 Jul 2005 07:44:59 +0200
From: "Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de>
Subject: Re: re-blessing a blessed reference
Message-Id: <slrnde8v2r.s1.tassilo.von.parseval@localhost.localdomain>

Also sprach Jon:

> I'm trying to tidy up my code a bit by creating an 'SQL' package. At the
> moment to interface with my db I'm doing something like..
>
> sub checkpass
> {
>         my ($username, $password) = @_;
>
>         my $dbh = DBI->connect("DBI:mysql:database", "username", "password")
>         unless ($dbh) { die($DBI::errstr); }
>
>         my $sql = "SELECT id FROM users WHERE username=? AND password=?";
>         my $sth = $dbh->prepare($sql);
>         $sth->execute( $username, $password) || die($sth->errstr());
>         
>         my $rows = $sth->rows;
>
>         $sth->finish();
>         $dbh->disconnect();
>
>         return $rows;
> }
>
> I'd like to be able to create a package so I only need to connect the
> database once and I can disconnect when the script has finished. eg:
>
> my $dbh = SQL->new();
>
> if ($dbh->checkpass()) { #authed } else { #notauthed }
>
> I've tried the code below but get it dies with "Can't locate object method
> "prepare" via package "SQL" at /SQL.pm line 19. I'm pretty new to OO so I
> can't figure out what I'm doing wrong. I understand that its trying to call
> on SQL::prepare but I don't know how I can make it not do that. I have a
> feeling its got something to do with me re-blessing $dbh which has already
> been blessed but would like someone with more experience to confirm that if
> possible. If I'm wrong could someone please point me in the right
> direction? Thanks for your time.

After re-blessing, the thing returned by DBI->connect is no longer an
instance of DBI::db. That's ok as long as your SQL package inherits
from DBI::db. See "Subclassing the DBI" in DBI's documentation.

So make SQL a subclass of 'DBI::db':

>=test.pl=
> #!/usr/bin/perl
>
> use lib './';
> use SQL;
> use strict;
>
> my $dbh = SQL->new();
> if ($dbh->checkpass( "username", "password" ))
> {
>         print "authed";
> }
> else
> {
>         print "not authed";
> }
>
>=SQL.pm=
> Package SQL;
>
> use DBI;
> use strict;
  use Carp;  # better use Carp::croak() instead of die()

  @SQL::ISA = qw/DBI::db/;  # make SQL a subclass of DBI::db

> sub new
> {
>         my $dbh = DBI->connect("DBI:mysql:database", "username", "password")

Don't retype code: This statement is missing a semicolon.

>         unless ($dbh) { die($DBI::errstr); }
>
>         bless $dbh, 'SQL';
>         return $dbh;
> }

The way 'new' is written forbids further subclassing of SQL. You might
not want to do that anyway, but it can't hurt to do it properly:

    sub new {
	my $class = shift;
	my $dbh = DBI->connect("DBI:mysql:database", "username", "password")
	    or croak $DBI::errstr;
	bless $dbh => $class;
    }

> sub checkpass
> {
>         my ($dbh, $username, $password);

Don't retype code: This statement is missing the assignment of @_.

>         my $sql = "SELECT id FROM users WHERE username=? AND PASSWORD=?";
>         my $sth = $dbh->prepare($sql);
>         $sth->execute( $username, $password ) || die($sth->errstr));
>         my $rows = $sth->rows;
>
>         return $rows;
> }
>
> 1;

Tassilo
-- 
use bigint;
$n=71423350343770280161397026330337371139054411854220053437565440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);


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

Date: Mon, 25 Jul 2005 17:59:00 +1200
From: Jon <wiseguy@ihug.co.nz>
Subject: Re: re-blessing a blessed reference
Message-Id: <dc1v0h$2ii$1@lust.ihug.co.nz>

Tassilo v. Parseval wrote:

> Also sprach Jon:
[ snip ]
> After re-blessing, the thing returned by DBI->connect is no longer an
> instance of DBI::db. That's ok as long as your SQL package inherits
> from DBI::db. See "Subclassing the DBI" in DBI's documentation.
> 
> So make SQL a subclass of 'DBI::db':
[ snip ]
>> sub new
>> {
>>         my $dbh = DBI->connect("DBI:mysql:database", "username",
>>         "password")
> 
> Don't retype code: This statement is missing a semicolon.
> 
>>         unless ($dbh) { die($DBI::errstr); }
>>
>>         bless $dbh, 'SQL';
>>         return $dbh;
>> }
> 
> The way 'new' is written forbids further subclassing of SQL. You might
> not want to do that anyway, but it can't hurt to do it properly:
> 
>     sub new {
> my $class = shift;
> my $dbh = DBI->connect("DBI:mysql:database", "username", "password")
> or croak $DBI::errstr;
> bless $dbh => $class;
>     }
> 

Thanks for your suggestions Tasillo, I'll look into them. My apologies for
retyping code, it seemed easier at the time but in hindsight probably
wasn't.
-- 
Jon
wiseguy@ihug.co.nz


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

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 8275
***************************************


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