Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am still fairly new to perl, but this one really puzzles me:
--Code Snippet--
sub remove_title { system "clear"; print "Enter Title you wish to remove (this will delete the .iso): +"; chomp($title = "default." . <STDIN>); print $title . "\n"; print "test"; print "test"; sleep(2); }

--Output--
Enter Title you wish to remove (this will delete the .iso):Saw
default.Saw

that's it nothing else prints or even works, I need to use $title to call a hash key and it has no clue. I use almost identical code in the main section which works fine. If there's something I'm missing I'm all ears

Replies are listed 'Best First'.
Re: Strange Behavior
by QM (Parson) on Mar 22, 2005 at 23:07 UTC
    You didn't print a final "\n", and autoflush is not enabled. The process dies before the output gets flushed.

    Add this to your code, somewhere before the last print:

    $|++;
    (There are other, more elegant ways to do this too.)

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      I don't understand why $|++ is universally preferred (or at least so it seems to me) over the more straightforward $|=1. What's the reason?

      What bugs me about $|++ is that it implies that there are levels of unbuffering, and that with $|++ we are getting more unbuffering than whatever we had before.

      And heaven forbid that some clown earlier on had the cute idea of unbuffering the selected handle with something like $|--. Come to think of it, $|++ seems to me every bit as bad as $|--.

      the lowliest monk

      Update: Fixed some awkward grammar.

        Can't speak for others, I use $|++ 'cause it's easier for me to see and find in Sources than $| = 1.

        Ordinary morality is for ordinary people. -- Aleister Crowley
        I don't understand why $|++ is universally preferred (or at least so it seems to me) over the more straightforward $|=1. What's the reason?
        Perhaps your "Perl Sense" is underdeveloped? $var++ can be either a counter or a boolean. It's the context that's important (as with so many things in Perl). I'd wager that most Perlers distinguish between the use of ++ and how the object is used.

        If you feel the need to be explicit, you might like this:

        use IO::Handle; STDOUT->autoflush(1); # OO form autoflush STDOUT 1; # procedural form

        -QM
        --
        Quantum Mechanics: The dreams stuff is made of

Re: Strange Behavior
by phaylon (Curate) on Mar 22, 2005 at 23:07 UTC
    Try adding $|++ in your script.

    Ordinary morality is for ordinary people. -- Aleister Crowley
Re: Strange Behavior
by Zaxo (Archbishop) on Mar 22, 2005 at 23:15 UTC

    This looks like an unimplemented stub. If the function were to delete a file, it would call unlink to do so.

    I would expect this to print

    default.Saw testtest
    If the function is to return the file name, it should end with the $title variable (which ought to be declared my). As it is, it returns the value returned by sleep, 2 in this case.

    # . . . sleep 2; $title; }

    After Compline,
    Zaxo

Re: Strange Behavior
by chas (Priest) on Mar 23, 2005 at 01:34 UTC
    I ran on Windows 98 with Perl v5.6.1:
    &remove_title; sub remove_title { system "cls"; print "Enter Title you wish to remove (this will delete the .iso):"; chomp($title = "default." . <STDIN>); print $title . "\n"; print "test"; print "test"; sleep(2); }
    Result:
    Enter Title you wish to remove (this will delete the .iso):xxx default.xxx testtest

    (The sleep occurs also.)
    chas
    (Have you looked at the script file with an editor in binary mode to check for any invisible characters which might be gumming things up? That's happened to me...Am I misunderstanding the problem?)
      Yes that is an unfinished code segment, I was just testing my subs out, I moved the data up into the switch statement, it magically worked, then back down to the sub and it worked again.
Re: Strange Behavior
by PsyberOne (Initiate) on Mar 23, 2005 at 03:07 UTC
    Here's the full code so you can get a better idea about where everything falls (note: not everything is fully implimented)
    #/usr/bin/perl use Config::Simple; use Switch; tie %DVD, "Config::Simple", '/home/mike/MyVideos/titles'; tie %Config, "Config::Simple", '/home/mike/MyVideos/config'; tied(%Config)->autosave(1); while () { system "clear"; print "---------OPTIONS-----------\n"; print "\'add\' to enter new title:\n"; print "\'delete\' to remove a title:\n"; print "\'import\' to rip a new DVD:\n"; print "\'quit\' to exit:\n"; print "---------------------------\n"; print "DVD Titles\n\n"; foreach (sort keys %DVD) { print "$_: \n"; } print "\nEnter the name of the movie you want to see:"; chomp($title = <STDIN>); switch ($title) { case ('add') { &add_title(); } case ('delete') { &remove_title(); } case ('import') { open(LSDVD, "lsdvd -p /dev/hdc|"); @lsdvd = <LSDVD>; shift(@lsdvd); print "$lsdvd[1]"; sleep(1); $lsdvd = $lsdvd[1]; @lsdvd = split(/ /, $lsdvd); $lsdvd = pop @lsdvd; print $lsdvd; $_ = $lsdvd; if (/(\w+)/) { print $!; } close(LSDVD); sleep(5); #&import_dvd(); } case ('quit') { &quit(); } else { $title = "default." . "$title"; $i =0; foreach (keys %DVD) { if ($title eq $_) { $i = 1; &play_movie(); } } if ($i eq 0) { system "clear"; print "Please enter a valid title!\n"; sleep(1); } } } } sub play_movie { print "Loading Movie...\n"; @mount = ("sudo mount" , "-o loop", "-t auto " , "\"$DVD{$title}\" +" , " /media/dvdbackup"); print @mount; system "@mount"; print "\nEnjoy!\n"; system("xine -I -A esd -B -D -f --no-splash --verbose=0 dvd:/media +/dvdbackup/1"); system("sudo umount /media/dvdbackup"); print "Thank you for watching.\n"; } sub add_title { system "clear"; print "Enter the name of the movie: "; chomp($name = <STDIN>); $name = "default." . "$name"; print "Enter the file location: "; chomp($loc = <STDIN>); print $name . $loc; tied(%DVD)->param( "$name" , "$loc" ); tied(%DVD)->write(); print "%DVD"; sleep(5) } sub import_dvd { } sub remove_title { system "clear"; print "Enter Title you wish to remove (this will delete the .iso): +"; chomp($title = "default." . <STDIN>); print $DVD{$title} . "\n"; print $title . "\n"; tied(%DVD)->delete("$title"); tied(%DVD)->write(); sleep(2); } sub quit { tied(%DVD)->write(); exit; }


    I realize the code is undoubtedly ugly, this is my first #real# program so be gentle. The sticky part now is in the import section. You guys are a life saver.
    By the way I didn't want to impliment unlink until I had the import code done (just incase).
      While I haven't read your code, two very important things seem to be missing from it:

      use strict; use warnings;

      These two will catch most of the errors you were likely to have. Not only in this program, in everything!

      (It is said that the universe itself uses strict...)