Finally a better script!

Discussion in 'Perl Misc' started by Nikos, Apr 29, 2005.

  1. Nikos

    Nikos Guest

    You understand very quickly what i wanted to do and you helped me write
    the correct way.
    After all of the guys suggestions here is how the script transformed:


    =========================================================
    my $script = param('select') || "Welcome Page!";
    my ($data, @data);


    if (param('select') and param('select') ne '..')
    {
    open(FILE, "<../data/text/$script.txt") or die $!;
    @data = <FILE>;
    close(FILE);

    $data = join('', @data);

    $dbh->do( "UPDATE guestlog SET script='$script' WHERE host='$host'"
    ) or die $dbh->errstr;
    }
    else
    {
    $sth = $dbh->prepare( "SELECT host FROM guestlog WHERE host=?" );
    $sth->execute($host);

    if ($sth->rows)
    {
    $sth = $dbh->prepare( "UPDATE guestlog SET
    hostcount=hostcount+1 WHERE host=?" );
    $sth->execute($host);

    $sth = $dbh->prepare( "SELECT * FROM guestlog WHERE host=?" );
    $sth->execute($host);
    $row = $sth->fetchrow_hashref;

    $data = "Êáëþò Þëèåò " .$host. "! ×áßñïìáé ðïõ âñßóêåò ôçí
    óåëßäá åíäéáöÃñïõóá!\n" .
    "Ôåëåõôáßá öïñÜ Þñèåò åäþ ùò " .$row->{host}. " óôéò "
    ..$row->{date}. " !!\n" .
    "ÓýíïëéêÃò Þñèåò åäþ " .$row->{hostcount}. " öïñÃò!!!\n" .
    "Ôåëåõôáßá åßäåò ôï êåßìåíï { " .$row->{script}. " }\n" .
    "Ãïéü êåßìåíï èá ìåëåôÞóåòé áõôÞí ôçí öïñÜ !?";
    }
    else
    {
    if ($host ne "Ãßêïò")
    {
    $data = "ÃåéÜ óïõ " .$host. "!\n" .
    "¸ñ÷åóáé ãéá 1ç öïñÜ åäþ !!\n" .
    "Åëðßæù íá âñåßò ôá êåßìåíá åíäéáöÃñïíôá :)";

    $sth = $dbh->prepare("SELECT * FROM guestlog WHERE host=?");
    $sth->execute($host);

    $sth = $dbh->prepare( "INSERT INTO guestlog (host, date,
    script, hostcount, pagecount) VALUES (?, ?, ?, ?, ?)" );
    $sth->execute($host, $date, $script, $hostcount, $pagecount);
    }
    else
    {
    $data = "ÃåéÜ óïõ Ãéêüëá, ôé ÷áìðÜñéá?! ¼ëá äåîéÜ íá óïõ
    ðÜíå ðÜíôá! ;-)";
    }
    }
    }
    =========================================================


    I wish i can test ti but iam currently having a mysql problem but thing
    it will work :)
    If we still can make it shorter please let me know!
    Nikos, Apr 29, 2005
    #1
    1. Advertising

  2. Nikos

    Henry Law Guest

    On Fri, 29 Apr 2005 22:07:54 +0300, Nikos <> wrote:

    >You understand very quickly what i wanted to do and you helped me write
    >the correct way.
    >After all of the guys suggestions here is how the script transformed:


    Well, you missed one of mine.

    >=========================================================
    >my $script = param('select') || "Welcome Page!";
    >my ($data, @data);
    >
    >
    >if (param('select') and param('select') ne '..')


    And I'm sure in my own mind that this statement isn't going to do what
    you want. For a start you do realise that "ne" is higher priority
    than "and" so this is going to evaluate (param('select') ne '..')
    first (yielding a true/false value), and then take the result and
    logically "and" it with the _same_ param('select'). I may be
    completely wrong and it is exactly what you want (I'm not a skilled
    Perl-ist myself) but I can't imagine what value of param('select')
    will yield sense in that statement.

    >{
    > open(FILE, "<../data/text/$script.txt") or die $!;


    Where did $script come from? Not this program, evidently.

    <snip>

    > $data = join('', @data);


    $data never seems to get used. All the occurrences of it from here on
    are on the left hand side of an assignment. I can't the point of
    slurping the contents of $script.txt in and jamming it all together.

    <snipped rest of the code>

    >I wish i can test ti but iam currently having a mysql problem but thing
    >it will work :)


    What? You're posting all this stuff and you haven't tested it? I'm
    wasting my time then. Sheesh.

    >If we still can make it shorter please let me know!


    Shorter != better.

    Now get this: Unless you post code that RUNS and doesn't contain
    unnecessary material unrelated to your question, and show YOUR output
    from it, and explain accurately why it's not doing what you thought it
    should do, then I'm not going to write another word to try to help
    you. Most other people have bailed out already.
    Henry Law, Apr 29, 2005
    #2
    1. Advertising

  3. Henry Law <> wrote:

    > On Fri, 29 Apr 2005 22:07:54 +0300, Nikos <>
    > wrote:
    >

    [snip]
    >>=========================================================
    >>my $script = param('select') || "Welcome Page!";

    ^^^^^^^

    [snip]
    >> open(FILE, "<../data/text/$script.txt") or die $!;

    >
    > Where did $script come from? Not this program, evidently.


    It was there, you just missed it. I'd guess that maybe you expected
    it to not be there. Understandable. :)


    > Now get this: Unless you post code that RUNS and doesn't contain
    > unnecessary material unrelated to your question, and show YOUR
    > output from it, and explain accurately why it's not doing what you
    > thought it should do, then I'm not going to write another word to
    > try to help you. Most other people have bailed out already.


    I've been lurking, not posting, but I also have little interest in
    trying to help someone who admits to being uninterested in Reading
    The Fine Manual and other helpful resources.
    David K. Wall, Apr 29, 2005
    #3
  4. Nikos <> wrote in news:d4u0m7$qjt$:

    > You understand very quickly


    Well, yes, I tend to, but who is this comment directed to?

    > my ($data, @data);


    Always declare variables in the smallest applicable scope. I am not sure
    what you want to do with these variables.

    See http://perl.plover.com/FAQs/Namespaces.html

    > open(FILE, "<../data/text/$script.txt") or die $!;
    > @data = <FILE>;
    > close(FILE);
    >
    > $data = join('', @data);


    1. Where did $script come from?

    2. If you just want to slurp the file, then do so:

    my $data;
    {
    my $fn = "/data/text/$script.txt";
    open my $file, '<', $fn or die "Cannot open $fn: $!";
    local $/;
    $data = <$file>;
    close $file or die "Cannot close $fn: $!";
    }

    $data will now contain the entire contents of /data/text/$script.txt.

    Or, you can use the excellent File::Slurp module.

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Apr 29, 2005
    #4
  5. Nikos

    Nikos Guest

    Nikos wrote: old_script [snip]

    Here is the script modified again implementing new ideas:

    <code>
    my %sql = (
    get_counter => "SELECT counter FROM visitorlog",
    get_host => "SELECT host FROM visitorlog WHERE host=?",
    update_counter => "UPDATE visitorlog SET counter+=1",
    update_visitorcounter => "UPDATE visitorlog SET visitorcounter+=1
    WHERE host=?",
    update_passage => "UPDATE visitorlog SET passage=? WHERE
    host=?",
    insert_host => "INSERT INTO visitorlog (host, date,
    passage, visitorcounter, counter) VALUES (?, ?, ?, ?, ?)",
    );

    my $passage = param('select') || "Ãñ÷éêÞ Óåëßäá!";
    my ($data, @data);


    if (param('select') and param('select') ne '..')
    {
    open(FILE, "<../data/text/$passage.txt") or die $!;
    @data = <FILE>;
    close(FILE);

    $data = join('', @data);

    $sth = $dbh->prepare( $sql{update_passage} );
    $sth->execute($passage, $host);
    }
    else
    {
    my $sth = $dbh->prepare( $sql{get_host} );
    $sth->execute($host);

    if ($sth->rows)
    {
    $sth = $dbh->prepare( $sql{update_host} );
    $sth->execute($host);

    $sth = $dbh->prepare( $sql{get_host} );
    $sth->execute($host);

    $row = $sth->fetchrow_hashref;
    $data = "Êáëþò Þëèåò " .$host. "! ×áßñïìáé ðïõ âñßóêåò ôçí
    óåëßäá åíäéáöÃñïõóá!\n" .
    "Ôåëåõôáßá öïñÜ Þñèåò åäþ ùò " .$row->{host}. " óôéò "
    ..$row->{date}. " !!\n" .
    "ÓýíïëéêÃò Þñèåò åäþ " .$row->{guestcounter}. "
    öïñÃò!!!\n" .
    "Ôåëåõôáßá åßäåò ôï êåßìåíï { " .$row->{passage}. " }\n" .
    "Ãïéü êåßìåíï èá ìåëåôÞóåòé áõôÞí ôçí öïñÜ !?";
    }
    else
    {
    if ($host ne "Ãßêïò")
    {
    $data = "ÃåéÜ óïõ " .$host. "!\n" .
    "¸ñ÷åóáé ãéá 1ç öïñÜ åäþ !!\n" .
    "Åëðßæù íá âñåßò ôá êåßìåíá åíäéáöÃñïíôá :)";

    $sth = $dbh->prepare( $sql{insert_host} );
    $sth->execute($host, $date, $passage, $guestcounter, $counter);
    }
    else
    {
    $data = "ÃåéÜ óïõ Ãéêüëá, ôé ÷áìðÜñéá?! ¼ëá äåîéÜ íá óïõ
    ðÜíå ðÜíôá! ;-)";
    }
    }
    }
    </code>

    I had trouble selecting good variable names but i think i have made good
    selections. Of course i changes the mysql columns names as well.

    I like your %hash idea a lot in fact i put all the sql statements there.

    I was also wondering if we can shorter it and perfect it even more maybe
    with subdivisions. :)
    Nikos, Apr 29, 2005
    #5
  6. Nikos

    Nikos Guest

    A. Sinan Unur wrote:

    > $data will now contain the entire contents of /data/text/$script.txt.


    Yes Sinan, that is also what i want because then a javascript follows to
    get the $data varibale and produce ncie effects.


    $data =~ s/\n/\\n/g;
    $data =~ s/"/\\"/g;
    $data =~ tr/\cM//d;

    print <<ENDOFHTML;
    <html><head><title></title>
    <script type='text/javascript'>

    var textToShow = "$data";

    {snip]

    I was wondering if there is a way to take out the js snippet from my
    index.pl but still pass the value of $data to that .js to produce the
    nice effects.....

    Also i have to take out the backslash and " symbols or otherwise the
    javascript part wont work correctly since $data is enclosed to double
    quotes.

    Is there soem better way to do this?
    Nikos, Apr 29, 2005
    #6
  7. * Henry Law schrieb:
    > Nikos <> wrote:
    >
    > > if (param('select') and param('select') ne '..')

    >
    > And I'm sure in my own mind that this statement isn't going to do what
    > you want. For a start you do realise that "ne" is higher priority
    > than "and" so this is going to evaluate (param('select') ne '..')
    > first (yielding a true/false value), and then take the result and
    > logically "and" it with the _same_ param('select'). I may be
    > completely wrong and it is exactly what you want (I'm not a skilled
    > Perl-ist myself) but I can't imagine what value of param('select')
    > will yield sense in that statement.


    Try to read this condition as: if param is true and not equal "..". Due
    to the very low precedence of »and« this construct makes sense, usually.

    Consider the undefined value as param. Just comparing *undef* with the
    string '..' throws a warning ("Use of uninitialized value in string ne
    at ..."). Hence one is checking if param is not the undefined value (ok,
    here the check tests for trueness instead of definedness).

    regards,
    fabian
    Fabian Pilkowski, Apr 29, 2005
    #7
  8. Nikos

    Nikos Guest

    Fabian Pilkowski wrote:
    > * Henry Law schrieb:
    >
    >>Nikos <> wrote:
    >>
    >>
    >>>if (param('select') and param('select') ne '..')


    Make the above if (param('select') and param('select') != '..') and its
    fine :)
    Nikos, Apr 30, 2005
    #8
  9. Nikos <> writes:
    > Fabian Pilkowski wrote:
    >> * Henry Law schrieb:
    >>
    >>>Nikos <> wrote:
    >>>
    >>>
    >>>>if (param('select') and param('select') ne '..')

    >
    > Make the above if (param('select') and param('select') != '..') and
    > its fine :)


    Um, no. Read 'perldoc perlop'-- and no, I don't care if you claim to
    have a hard time with it. I won't read it for you. At the very
    least, you should be able to search it for != and ne and find out why
    they're different, and why you're wrong.

    -=Eric
    --
    Come to think of it, there are already a million monkeys on a million
    typewriters, and Usenet is NOTHING like Shakespeare.
    -- Blair Houghton.
    Eric Schwartz, Apr 30, 2005
    #9
  10. * Nikos schrieb:
    >
    > <script type='text/javascript'>
    > var textToShow = "$data";
    >
    > {snip]
    >
    > I was wondering if there is a way to take out the js snippet from my
    > index.pl but still pass the value of $data to that .js to produce the
    > nice effects.....
    >
    > Also i have to take out the backslash and " symbols or otherwise the
    > javascript part wont work correctly since $data is enclosed to double
    > quotes.
    >
    > Is there soem better way to do this?


    Yes, it is. I've mentioned it in another of your threads. Admittedly, it
    is hard to find, there are so many (and long) threads you started the
    last days. Perhaps you want to stop that -- it would be easier to find
    information one has given to you, then. Read my posting with message-id

    <news:>

    again (the last sentences concern about this). Meanwhile, it's too often
    that you don't take the information one gives you. You're asking and
    asking but don't take care of the answers. I've answered this question
    already but you're going to ask it once again. Why? I don't understand.

    OTOH, I suggest you do your page without JavaScript -- it's hard enough
    to learn Perl and HTML first. Once you are familiar with them you could
    add some JavaScript lessons to your timetable.

    And, of course, this group is not for talking about JavaScript nor any
    detail you need to set up your website with that language inside.

    regards,
    fabian
    Fabian Pilkowski, Apr 30, 2005
    #10
  11. Nikos

    Nikos Guest

    Fabian Pilkowski wrote:
    > * Nikos schrieb:
    >
    >><script type='text/javascript'>
    >>var textToShow = "$data";
    >>
    >>{snip]
    >>
    >>I was wondering if there is a way to take out the js snippet from my
    >>index.pl but still pass the value of $data to that .js to produce the
    >>nice effects.....
    >>
    >>Also i have to take out the backslash and " symbols or otherwise the
    >>javascript part wont work correctly since $data is enclosed to double
    >>quotes.
    >>
    >>Is there soem better way to do this?

    >
    >
    > Yes, it is. I've mentioned it in another of your threads. Admittedly, it
    > is hard to find, there are so many (and long) threads you started the
    > last days. Perhaps you want to stop that -- it would be easier to find
    > information one has given to you, then. Read my posting with message-id
    >
    > <news:>
    >
    > again (the last sentences concern about this). Meanwhile, it's too often
    > that you don't take the information one gives you. You're asking and
    > asking but don't take care of the answers. I've answered this question
    > already but you're going to ask it once again. Why? I don't understand.


    True but i have quit that.
    If you see my posts from yesterday and on i *do* listen.

    > OTOH, I suggest you do your page without JavaScript -- it's hard enough
    > to learn Perl and HTML first. Once you are familiar with them you could
    > add some JavaScript lessons to your timetable.


    No need to learn Javascript. I wont be using this except this
    char_by_char nice producing effect i want.
    I must pass the value though to js if i put in in a diff file, how?
    Nikos, Apr 30, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VB Programmer

    Question: Try,Catch,Finally

    VB Programmer, Aug 7, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    359
    Kevin Spencer
    Aug 7, 2003
  2. Peter Bencsik
    Replies:
    2
    Views:
    812
  3. David Lozzi

    Try...Catch...Finally not firing finally?

    David Lozzi, Apr 23, 2007, in forum: ASP .Net
    Replies:
    12
    Views:
    783
    Alvin Bruney [MVP]
    May 11, 2007
  4. Andrew Thompson
    Replies:
    8
    Views:
    135
    Premshree Pillai
    Jun 7, 2005
  5. Replies:
    2
    Views:
    44
    Mark H Harris
    May 13, 2014
Loading...

Share This Page