Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^5: Perl/Tk code structure

by BrowserUk (Pope)
on Jan 18, 2012 at 19:31 UTC ( #948608=note: print w/ replies, xml ) Need Help??


in reply to Re^4: Perl/Tk code structure
in thread Perl/Tk code structure

It turns out that if I have something like open(LOG, ">:encoding(UTF-8)", "$scriptpath/scripts/log.txt") or print "\nCan't create log file: $!\nContinuing anyway.\n"; before the .pm is loaded, it crashes. (It works if I remove ">:encoding(UTF-8)", but that's hardly ideal.)

It appears that the unicode IO layers are not thread-safe. I cannot help you with that, you'll have to raise a bug report.

The work around is to not open your logfile until after you've decided whether to invoke the GUI or not.

If you find that too restricting, then open the logfile without the encoding layer before making the decision and binmode the io layer on afterward:

#! perl -slw use strict; use threads; our $GUI //= 0; open LOG, ">", "$0.log" or print "Can't create log file: $!"; if( $GUI ) { require 'MyGui.pm'; async( \&MyGui::gui )->detach; } else { binmode STDIN, ':encoding(UTF-8)'; } binmode LOG, ':encoding(UTF-8)'; while( 1 ) { printf 'Enter three, 2-digit numbers: '; my $in = scalar <STDIN>; last if $in =~ '!bye'; print 'You entered: ', $in; printf 'Enter a date and time: '; $in = scalar <STDIN>; last if $in =~ '!bye'; print 'You entered: ', $in; }
I also have binmode STDIN, ':encoding(UTF-8)'; at the start of the script, which also crashes the GUI (IIRC it was needed for non-ASCII input file names to work on linux).

If you are using the gui, then you will not actually be using STDIN, as the input will come via Tk. So, only binmode stdin if you are not using the gui, as shown above.

From what I can tell, the thread-hostility of the unicode IO layers is not a some subtle error involving race conditions or mismatched locking, but rather a complete absence of any attempt on behalf of the code to be thread-safe. Just plain, old-fashioned bad coding.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?


Comment on Re^5: Perl/Tk code structure
Select or Download Code
Re^6: Perl/Tk code structure
by elef (Friar) on Jan 21, 2012 at 19:39 UTC
    Thanks for all the help. The project is coming along pretty nicely, although I'll need to work on error reporting in the GUI and stuff like that.
    I'm not sure which option to choose for launching the GUI, but both are workable and shouldn't be too difficult to implement.
      I'm not sure which option to choose for launching the GUI, but both are workable and shouldn't be too difficult to implement.

      It's not clear to me which "two options" you mean, but you seem to have a handle on it, so good luck.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

        a) Command line argument provided by the user when launching the script.
        b) Keep what I have now, i.e. platform-specific defaults set in the main script, which can be overridden via setup.txt. I'd need to work around the encoding bugs to get this working.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://948608]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2014-07-10 05:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (199 votes), past polls